Java starts from scratch 21 (Collection list interface)

Source: Internet
Author: User

The list interface list is a sub-interface of the Collection, which can save each duplicate content, this interface is defined as follows: public interface list<e> extends collection<e> two, Extension methods for list interfaces
No. Method Type Describe
1 public void Add (int index, E Element) Ordinary Add an element at a specified position
2 public boolean addall (int index, COLLECTION<? extends e> c) Ordinary Adds a set of elements to a specified position
3 E get (int index) Ordinary Returns the element at the specified position
4 public int indexOf (Object o) Ordinary Find the location of the specified element
5 public int lastIndexOf (Object o) Ordinary Finds the position of the specified element from the back forward
6 Public listiterator<e> Listiterator () Ordinary Instantiating a Listiterator interface
7 Public E Remove (int index) Ordinary Delete an element at the specified location
8 Public list<e> sublist (int fromIndex, int toindex) Ordinary Take out the subset in the collection
9 Public E Set (int index, e Element) Ordinary Replaces the element at the specified position
The common subclass--arraylist and LinkedList of the list interface ArrayList implements a variable-length array, allocating connected space in memory, and traversing and randomly accessing elements with high efficiency The LinkedList uses the list storage method. High efficiency when inserting and deleting elements Four,ArrayList Example
 PackageCom.pb.demo1; Public classPerson {PrivateString name; Private intAge ;  PublicPerson () {} PublicPerson (String name,intAge ) {     This. Name =name;  This. Age =Age ;} PublicString GetName () {returnname;} Public voidsetName (String name) { This. Name =name;} Public intGetage () {returnAge ;} Public voidSetage (intAge ) {     This. Age =Age ;} }

 PackageCom.pb.demo1;Importjava.util.ArrayList;Importjava.util.List; Public classPersonarraylisttest { Public Static voidMain (string[] args) {/** Create multiple person objects and assign values*/Person P1=NewPerson ("Zhang San", 21); Person P2=NewPerson ("John Doe", 22); Person P3=NewPerson ("Harry", 23); //Create ArrayListList<person> personlist=NewArraylist<person>(); //to add an object to a collectionPersonlist.add (p1);       Personlist.add (p2);       Personlist.add (p3); //the length of the output collectionSystem.out.println ("Set Length:" +personlist.size ()); //iterating through the collection for loop        for(inti=0; I<personlist.size (); i++) {System.out.println ("Name:" +personlist.get (i). GetName () + "Age:" +Personlist.get (i). Getage ()); }       //using an index to addPerson P4 =NewPerson ("Zhao Liu", 24); /** Must know the length when using the index to increase correctly, if the added index is greater than the last subscript +1, * Compile can pass but the runtime will error * For example: if the length of the check is 3, the subscript is from 0 to 2, then use the index to add a value index can be (0 Yes, but not 4.*/Personlist.add (0, p4); //the length of the output collectionSystem.out.println ("Set Length:" +personlist.size ()); ////iterating through the collection Forearch loops        for(person p:personlist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); }              //find if there is a P4 object in the collection true represents there, false means noSystem.out.println ("Find a P4 object in the collection:" +Personlist.contains (p4)); //The subscript that the P2 element is in front of backwardsSystem.out.println ("The P2 element is located in front of the back:" +Personlist.indexof (p2)); //The p2 element is in the subscript from the back forwardSystem.out.println ("The P2 element is the subscript from the back forward:" +Personlist.lastindexof (p2)); }}
v. Examples of LinkedList

 PackageCom.pb.demo1;Importjava.util.LinkedList;Importjava.util.List; Public classPersonlinkedlisttest { Public Static voidMain (string[] args) {/** Create multiple person objects and assign values*/Person P1=NewPerson ("Zhang San", 21); Person P2=NewPerson ("John Doe", 22); Person P3=NewPerson ("Harry", 23); Person P4=NewPerson ("Zhao Liu", 24); Person P5=NewPerson ("Money seven", 25); //Create LinkedListlinkedlist<person>plist=NewLinkedlist<person>();         Plist.add (p1);         Plist.add (p2);         Plist.add (p3); //Set LengthSystem.out.println ("Set Length" +plist.size ()); //iterating through the collection          for(person p:plist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); }        //add an object to the first and lastPlist.addfirst (p4);         Plist.addlast (p5); System.out.println ("=========== Add Data ================="); //Set LengthSystem.out.println ("Set Length" +plist.size ()); //iterating through the collection          for(person p:plist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); }               //Gets the element that specifies the subscript: subscript to already exist if not present after running will errorSystem.out.println ("Gets the element with the specified subscript:" +plist.get (4). GetName ()); System.out.println ("=========== Remove ================= by subscript"); //Remove The element remove, and press the subscript removalPlist.remove (0); //Set LengthSystem.out.println ("Set Length" +plist.size ()); //iterating through the collection          for(person p:plist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); } System.out.println ("=========== directly Remove Object ================="); //Remove an element remove directly from the object,plist.remove (p2); //Set LengthSystem.out.println ("Set Length" +plist.size ()); //iterating through the collection          for(person p:plist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); } System.out.println ("=========== Remove the first or last ================="); //Remove An element remove the first or lastPlist.removefirst ();        Plist.removelast (); //Set LengthSystem.out.println ("Set Length" +plist.size ()); //iterating through the collection          for(person p:plist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); } System.out.println ("=========== Remove all ================="); //Remove all elements Clear methodplist.clear (); //Set LengthSystem.out.println ("Set Length" +plist.size ()); //iterating through the collection          for(person p:plist) {System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); }             }}
The vector class also has a subclass in the list interface: The Vector,vector class belongs to a saved subclass, from the entire Java collection development history, the vector is an elder class, at JDK 1.0 already exist class this class. However, after the Java 2 (JDK 1.2) focused on the concept of the collection framework, so the definition of a lot of new interfaces (such as list, etc.), but considering that a large number of people have become accustomed to the use of vector classes, so the Java designers, Let the vector class implement a list interface, which will keep it. But because it is a list subclass, the use of the vector class is not much different from the previous one.
 PackageCom.pb.demo1;ImportJava.util.Vector; Public classVectortest { Public Static voidMain (string[] args) {Vector<String> plist=NewVector<string>(); Plist.add ("Zhang San"); Plist.add ("John Doe"); Plist.add ("Harry"); Plist.add ("Zhao Liu"); System.out.println ("Length:" +plist.size ());  for(String s:plist) {System.out.println (s); }    }}

Java starts from scratch 21 (Collection list interface)

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.