/*list Basic Operation
*
* List A=new list ();
* Increase
* A.add (index,element); add by specified position, the remaining elements move back
* AddAll (index,collection); Adds a series of elements starting at the specified position, and the remaining elements move back
* Delete
* A.remove (index); Delete the specified position element
* Change
* A.set (index,element); change the specified position element
* Check
* A.get (index); Gets the specified position element
* Sublist (FROM,TO); Returns a list sequence
* Listiterator ();
* Returns a listiterator iterator: The iterator, which can be used for additional deletions and checks,
*
*
* Note: When iterating over data, it is not possible to manipulate the data through the collection, only through the iterator to manipulate the data
* But iterator only remove (), Next (), Hasnext () three methods,
* So to use the Listiterator interface, to achieve additions and deletions to change the operation
*
* Reason: Because the elements in the list are all ordered, you can do the additional deletions
*
* */
1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 Importjava.util.List;4 ImportJava.util.ListIterator;5 6 7 Public classStudentcode {8 9 Public Static voidMain (String []args) {Ten //Defining a List Object OneArrayList al=NewArrayList (); A //add element 1 (add back one at a time) -Al.add ("Java01"); -Al.add ("Java02"); theAl.add ("Java03"); -Al.add ("Java04"); - System.out.println (AL); - //add element 2 (add at established location) +Al.add (1, "java09"); - System.out.println (AL); + //deletes the specified position element. AAl.remove (2); at System.out.println (AL); - //modifying elements -Al.set (2, "java10"); - System.out.println (AL); - //Enquiry - //get elements with a corner mark inSystem.out.println ("get (1) =" +al.get (1)); - //get all elements (corner bid get)-----for Loop to //iterators + //Listiterator - the for(Listiterator it=al.listiterator (); It.hasnext ();) { *Object obj=It.next (); $ if(Obj.equals ("java09"))Panax NotoginsengIt.add ("Java11");//modifying an element can only be done through an iterator - //it.set ("Java11"); the } +System.out.println (AL);//There are elements in the object as well. A //Query element location ———— indexOf theSystem.out.println ("Java09 index=" +al.indexof ("java09")); +List sub=al.sublist (1, 3); - System.out.println (sub); $ } $}
Java Collection Framework list operation