Java Array Traversal Delete remove (sample code) _java

Source: Internet
Author: User
Tags getcolor
Nonsense not much to say that directly on the code
Copy Code code as follows:

Package com.b;

Import java.util.ArrayList;

Array traversal deletion, adding
public class Core2 {
private String name;
private int num;
private String color;

Public Core2 () {
}

Public Core2 (String a, int B, string c) {
name = A;
num = b;
color = c;
}

Public String GetName () {
return name;
}

Public String GetColor () {
return color;
}

public int Getnum () {
return num;
}

public static void Main (string[] args) {
Core2 C = new Core2 ("Little Red", 13, "red");//Create 3 instances
Core2 C2 = new Core2 ("Xiaoming", 12, "yellow");/= Object
Core2 C3 = New Core2 ("Cockroach", 123, "Blue");
Core2 C4 = new Core2 ("Small Li", 23, "white");
Core2 c5 = new Core2 ("Little Love", 4, "Blue");
Core2 C6 = new Core2 ("Small North", 2, "brave");
ArrayList AA = new ArrayList ();//Collection Container
Aa.add (c);
Aa.add (C2);
Aa.add (C3);
Aa.add (C4);
Aa.remove (1);
Aa.add (2, C5);
Aa.add (1, C6);
Aa.remove (C4);
SYSTEM.OUT.PRINTLN ("size is:" + aa.size ());
for (int i = 0; i < aa.size (); i++) {
Core2 m = (Core2) aa.get (i)//ArrayList traversal and increase
System.out.println ("No." + (i + 1) + "Student Name:" + m.getname () + ", School number is:"
+ m.getnum () + ", Color is:" + m.getcolor ());

}

}

}
Size is: 5
1th student Name: Xiao Hong, school number is: 13, the color is: red
2nd student Name: Xiao Bei, school number is: 2, the color is: brave
3rd student Name: Xiao Ming, school number is: 12, the color is: yellow
4th Student Name: Little Love, School number is: 4, the color is: Blue
5th student Name: Xiao Qiang, school number is: 123, the color is: Blue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.