Unique features of the list collection:
A: Add Features
void Add (int index,object Element): add element at the specified index
B: Get Features
Object get (int index): Gets the element at the specified index
C: List iterator
Listiterator Listiterator (): A list collection-specific iterator
D: Delete function
Object Remove (int index): Deletes elements based on index, returns deleted elements
E: Modify function
Object set (int index,object element): Modifies the element according to the index, returning the decorated element
1 PackageZl_objecttest1;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 ImportJava.util.ListIterator;6 7 Public classListDemo2 {8 9 Public Static voidMain (string[] args) {Ten One //first create a collection AList LT =NewArrayList (); - - //adding elements to a collection theLt.add ("I"); -Lt.add ("Can"); -Lt.add ("Do"); - + //A: Add function void Add (int index,object Element): Add elements at the specified index location - //lt.add (0, "do It");//[Do it, I, Can, do] Adds an element at the No. 0 index at the specified position, and the original element of the index is moved backward + //Lt.add (1, "do It");//[I, do it, Can, do] A //Lt.add (2, "do It");//[I, Can, do it, do] at //Lt.add (3, "do It");//[I, Can, do, do it] it's no problem with the same length index. - //Lt.add (4, "do It");//error because the size of this collection is only 3, and 4 exceeds the length of the collection - System.out.println (LT); - - - //B: Get function Object get (int index): Gets the element at the specified position in //System.out.println (Lt.get (2)); - //System.out.println (Lt.get (3));//error because there are no elements at the 3 index to + //C: List iterator Listiterator listiterator (): List collection-specific iterator - /*listiterator ltr = Lt.listiterator (); the //Traversal * While (Ltr.hasnext ()) { $ string s = (string) ltr.next ();Panax Notoginseng System.out.println (s); - }*/ the + //D: Remove function Object remove (int index): Deletes elements based on index, returns deleted elements A //System.out.println (lt.remove (0));//I delete I, then this set becomes Can do the //System.out.println (Lt.remove (2)); + //Do This is the result of commenting out the last sentence, otherwise it is an error, because after the last sentence runs, the collection length becomes 2, the index is only 0, 1 - $ //E: Modify function Object set (int index,object element): Modifies element according to index, returns the decorated element $ //System.out.println (lt.set (0, "you")); - //return I, the description I has been replaced by you. If you are iterating through the collection, then it is: Can do - the } - Wuyi}
Methods of the Java 15-6 list