List container -- ArrayList and common APIs, listarraylistapi
List:
① The List container is an ordered collection (also called a sequence ). Users of this interface can precisely control the insert position of each element in the List container. You can access the element based on the integer index (position in the list) of the element and search for the element in the list. The List container allows repeated values to be inserted, including null;
② The most common implementation classes of the two List interfaces are ArrayList and listlist;
ArrayList and common APIs:
① ArrayList-dynamic array;
② The ArrayList class extends javasactlist and implements the List interface;
③ Support dynamic arrays that can grow as needed;
④ ArrayList constructor:
A) ArrayList ()
B) ArrayList (Collection c)
C) ArrayList (int capacity)
⑤ Apart from the inherited methods, common ArrayList methods:
A) E get (int index) returns the elements at the specified position in this list
B) int indexOf (Object o) returns the index of the specified element that appears for the first time in the list, or-1 if the list does not contain any element.
C )......
Add, modify, and output ArrayList
1 List <String> nList = new ArrayList <String> (); 2 nList. add ("zhangsan"); // add the specified element to the end of the list 3 nList. add ("lisi"); 4 nList. add ("wangwu"); 5 nList. add (1, "jay"); // Insert the specified element into the specified position 6 nList in this list. set (0, "Ali"); // use the specified element to replace the element 7 System at the specified position in the list. out. println ("using Iterator objects for unified traversal"); 8 Iterator <String> it = nList. iterator (); 9 while (it. hasNext () {10 String name = it. next (); 11 System. out. println (name); 12} 13 14 System. out. println ("use enhanced for loop for unified traversal"); 15 for (String name: nList) {16 System. out. println (name); 17}
Output result:
Use iterator objects for unified Traversal
Ali
Jay
Lisi
Wangwu
Use an enhanced for loop for unified Traversal
Ali
Jay
Lisi
Wangwu
Common method for surface connection testing:
1 System. out. println ("************************************* ***"); 2 System. out. println (nList. indexOf ("lisi"); // returns the index of the specified element that appears for the first time in the list, or-1 if the list does not contain the element. 3 System. out. println (nList. remove ("lisi"); // remove the specified element that appears for the first time in this list (if any ). 4 System. out. println (nList. remove (0); // remove the element 5 System at the specified position in this list. out. println (nList. size (); // returns the number of elements in the list. -- There are four, and two are deleted. The result is 2 6 System. out. println (nList. contains ("zhangsan"); // if the list contains the specified element, true is returned. 7 System. out. println (nList. get (0); // return the elements at the specified position in the list. 8 System. out. println (nList. isEmpty (); // returns true 9 nList if the list contains no elements. clear (); // remove all elements in this list 10 System. out. println (nList. isEmpty ());
Output result:
****************************************
2
True
Ali
2
False
Jay
False
True
Create a class and add
1 class Student{2 private String name;3 private int age;4 }
Add the get and set methods to it.
The get and set methods will be created automatically.
1 public String getName() { 2 return name; 3 } 4 public void setName(String name) { 5 this.name = name; 6 } 7 public int getAge() { 8 return age; 9 }10 public void setAge(int age) {11 this.age = age;12 }
Create a constructor with two parameters for it
1 public Student(String name, int age) {2 super();3 this.name = name;4 this.age = age;5 }
Add elements and operations in the main method
1 List<Student> stuList=new ArrayList<Student>(); 2 Student stu1=new Student("zhangsan", 10); 3 Student stu2=new Student("lisi", 20); 4 Student stu3=new Student("wangwu", 30); 5 Student stu4=new Student("zhaoliu", 25); 6 Student stu5=new Student("tianqi", 15); 7 stuList.add(stu1); 8 stuList.add(stu2); 9 stuList.add(stu3);10 stuList.add(stu4);11 stuList.add(stu5);12 Student stu6=new Student("tianqi", 15);13 System.out.println(stuList.indexOf(stu6));//-1
But we want to return the index when the name and age are the same;
To view the indexOf in ArrayList, use the following method: (view the method ctrl + left-click to select ArrayList, and then find the indexOf (Object) method in the Outline View ):
1 public int indexOf(Object o) { 2 if (o == null) { 3 for (int i = 0; i < size; i++) 4 if (elementData[i]==null) 5 return i; 6 } else { 7 for (int i = 0; i < size; i++) 8 if (o.equals(elementData[i])) 9 return i;10 }11 return -1;12 }
The equals comparison is used here, which means that each student creation cannot be the same, so we need to reconstruct the equals method in the Student class;
In Eclipse, the equals reconstruction method is also provided:
1 @Override 2 public boolean equals(Object obj) { 3 if (this == obj) 4 return true; 5 if (obj == null) 6 return false; 7 if (getClass() != obj.getClass()) 8 return false; 9 Student other = (Student) obj;10 if (age != other.age)11 return false;12 if (name == null) {13 if (other.name != null)14 return false;15 } else if (!name.equals(other.name))16 return false;17 return true;18 }
Modify the Student class before testing:
1 System. out. println (stuList. indexOf (stu6); //-12 System. out. println (stuList. contains (stu6); 3 System. out. println (stuList. remove (stu6); // Delete using the equals method in remove, all stu5 will be deleted together 4 System. out. println (stuList. indexOf (stu5); 5 System. out. println (stuList. size ());
The output result is:
-1
False
False
4
5
After the equals () is reconstructed, the output is:
Output result:
4
True
True
-1
4