I. Definition
Collections (Collection) are a common way of storing objects in Java, and each object in the collection is ultimately stored in a similar array []. So why do arrays have to use collections? Because the length in the collection is variable, you do not have to define its length at the beginning (the array must define its length when it is defined), the array can store the base data type, and the collection can store arbitrary objects.
The Java Collection (collection) contains list and set; This chapter first describes the list collection
Two. List category
ListCommonCan also include: Arraylit,linkedlist,vecor;
Three. List common operations
The following is a list of common ways to do this with ArrayList.
-
A list belongs to one of the set, its length is variable, the value is variable, and the content can be any object
- The list result is an array-like format and can also be seen as a list in a similar Pyhotn
- The list is ordered and its value can be repeated, so it can be manipulated using an index
- The set is unordered, so its value cannot be duplicated.
Methods in the list
Generating ArrayList objects
ArrayList ArrayList = new ArrayList ();
1. Increase
Arraylist.add () is added by default at the last location
Arraylist.add (int index,object obj) is added at the specified location
2. By deleting
Arraylist.remove (int index) is removed by index
Arraylist.remove (object) is removed according to the value object, and if there are multiple identical objects, the first one is deleted sequentially.
3. Change
Arraylist.set (int index,object obj) sets its value by the specified index
4. Check
Arraylist.get (int index) gets the corresponding value by index
Arraylist.index (Object obj) gets the corresponding index value according to the specific object, and if the same object gets the first corner label
Arraylist.lastindex (Object obj) gets the corresponding last index value according to the specific object (reverse arrangement)
5. Traversing the list
5.1 Using a For loop
for (int i=0;i<arraylist.size (); i++) {
System.out.println (Arraylist1.get (i));
}
5.2 Using Iterators iterator
Iterator it = Arraylist.iterator ();
5.2.1 method 1s using a for loop
for (; It.hasnext ();) {
System.out.println (It.next ());
}
5.2.2 Method 2 using the while loop
while (It.hasnext ()) {
System.out.println (It.next ());
}
6. Compare
7. Judging
List common operations for the Java collection