Java List Collection-specific method program usage

Source: Internet
Author: User

Package Collection;/*collection|--list: Elements are ordered and elements can be duplicated. Because the collection system has an index | | --arraylist: The underlying data structure uses the structure of the array features: query, modify the speed quickly, but additions and deletions slightly slower. Thread is out of sync | | --linkedlist: The underlying uses the data structure features of the linked list: Delete quickly, query slightly slow | | --vector: The bottom layer is the array data structure. Thread synchronization. Replaced by the ArrayList |--set: The element is unordered, the element can be repeated list: A unique method of the collection, usually the method of operation of the angle is the system-specific method of adding add (index,element); AddAll (index,collection) ; Remove (index), change set (index,element), check get (index), sublist (from, to), Listiterator (); An iterator that is unique to the List collection. Listiterator is a sub-interface of iterator when iterating, the elements in the collection cannot be manipulated through the collection object's methods. Because the concurrent~~~ exception occurs. Therefore, in the iteration, only with the method of the iterator operation elements, but the Iterator method is limited, can only judge the elements, take out, delete the operation. If you want other operations such as add, modify, etc., use its subinterface, Listiterator. This interface can only be obtained through the Listiterator method of the list collection. */import java.util.*;p ublic class listdemo{/** * @param args */public static void sop (Object obj) {System.out.println (obj) ;} public static void Method () {arraylist<string> al = new arraylist<string> ();//Addal.add ("Java01"); Al.add (" Java02 "), Al.add (" java03 "), SOP (" Original set: "+al);//add Element Al.add (1," java09 ") at specified location,//delete specified element Al.remove (2);//Modify element Al.set (0," Java10 ");//Get element sop via Corner Mark (" Get (1): "+al.get (0); Sop (AL);//Get all elements for loop for (int x = 0;x<al.size (); x + +) {System.out.println ("Al" ("+x+") = "+al.get (x)");} Iterator iterator<string> it = Al.iterator (); while (It.hasnext ()) {SOP ("Next:" +it.next ());} Obtain the position of the object by IndexOf sop ("indexof =" +al.indexof ("Java10")); list<string> sub = al.sublist (0, 2); SOP ("sub =" +sub);} public static void Main (string[] args) {//TODO auto-generated method stub//Demo List iterator arraylist<string> alarraylist = n EW arraylist<string> (); Alarraylist.add ("Java01"); Alarraylist.add ("Java02"); Alarraylist.add ("Java03"); SOP ( alarraylist);//Use listiterator interface listiterator<string> li = Alarraylist.listiterator ();//There is no previous element//SOP (" Hasprevious () "+li.hasprevious ()"), while (Li.hasnext ()) {Object obj = Li.next (), if (Obj.equals ("Java01")) {//Add//Li.add ("Java11");//Modify Li.set ("Java11");}} while (Li.hasprevious ()) {SOP ("Pre::" +li.previous ());} There is no latter element//SOP ("Hasnext ():" +li.hasnext ());//SOP ("hasprevious ()" +li.hasprevious ()), SOP (alarraylist);/* * Use Iterator interface//During the iterative process,Ready to add or remove elements iterator<string> iterator = Alarraylist.iterator (); while (Iterator.hasnext ()) {//SOP ("Next:" + Iterator.next ()), Object obj = Iterator.next (), if (Obj.equals ("Java01")) {//Alarraylist.add ("Java11");//Will JAVA01   The Iterator.remove () is removed from the collection by a reference to the }sop ("OBJ =" +obj);} */}}

  

Java List Collection-specific method program usage

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.