List Common methods:
Default add: List.add (e);
Specify the subscript to add (after adding the element after subscript, move one bit backwards): List.add (index,e);
Get the number of elements in the set: List.size ();
Returns whether deleted: List.remove (e);
Delete the element that specifies the subscript directly (delete only the first matching element found): List.remove (index);
Replace element (replaces the element with the specified subscript): List.set (index,e);
remove element: List.get (index);
Empty collection: List.clear ();
Determines whether an element exists in the collection (there is a return of true, there is no return false): List.contains (e);
Two objects must be equal: List.equals (LIST2);
Two objects are not necessarily equal: list.hashcode () = = List2.hashcode ();
(The Equals method of two equal objects must be true, but two hashcode equal objects are not necessarily equal.)
The element exists then returns the subscript of the first element found, and returns -1:list.indexof (e) If it does not exist;
The element exists then returns the subscript of the last element found, and returns -1:list.lastindexof (e) If it does not exist;
Determines whether the collection is empty (null returns TRUE, non-null returns FALSE): List.isEmpty ();
Returns the Iterator Collection object: List.iterator ();
- list<string> list = new arraylist<string> ();
List.add ("1234");
List.add ("12345");
List.add ("12346");
List.add ("12347");
List.add ("12348");
Iterator<string> it = List.iterator ();//Traversal
while (It.hasnext ()) {
System.out.println (It.next ());
}
Converts a collection to a string: list.tostring ();
To convert a collection to an array:
Default type: List.toarray ();
Specifies the type (objects is an array object of the specified type and assigns the transformed array to the objects array): List.toarray (objects);
- List Traversal Method
- public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("AA");
List.add ("BB");
List.add ("CC");
List.add ("DD");
The first traversal method of the list
for (String str:list) {
System.out.print (str+ "");
Print AA BB cc DD
}
System.out.println ();
The second traversal method of list
for (int i=0;i<list.size (); i++) {
if (I!=list.size ()-1) {
System.out.print (List.get (i) + "");
This remove (i) is removed from the printed
List.remove (i);
}else{
System.out.print (List.get (i));
}
Print as AA cc
}
The third kind of traversal method for list
System.out.println ();
For (iterator<string> iterator=list.iterator (); Iterator.hasnext ();) {
if (Iterator.hasnext ()) {
System.out.print (Iterator.next () + "");;
}else{
System.out.print (Iterator.next ());;
}
}
Print AA BB cc DD
}
List of common methods for list map set