Java Collection Framework list operation

Source: Internet
Author: User

/*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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.