List interface
The list interface is defined as follows:
Public Interface List<e>extends collection<e>
Can find the list interface when the collection interface sub-class, the list interface a large number of expansion of the operation of the collection interface, the contents of the list interface is allowed to repeat.
Common methods for list interfaces
The function of these methods is summed up in one sentence: In order to realize the function of adding a limitation check .
The following method:
voidAddintindex,e Element)//inserts the specified element (optional action) at the specified position in the list. Moves the element that is currently in that position (if any) and all subsequent elements to the right (plus 1 in its index). Boolean AddAll (intindex, COLLECTION<? Extends e>c)//inserts all the elements in the specified collection into the specified position in the list (optional action). EGet(intIndex//returns the element at the specified position in the list. intindexOf (Object o)//returns the index of the specified element that first appears in this list, or 1 if the list does not contain the element .ESet(intindex,e Element)//replaces the element in the specified position in the list with the specified element (optional action). boolean remove (Object o)//removes the first occurrence of the specified element (if one exists) from this list (optional action). If the list does not contain elements, the list is not changed.
There are other ways, of course, except for the above.
Subclass of List Interface-arraylist (common)
ArrayList is defined as follows:
Public class Arraylist<e>extends abstractlist<e>implements List<e>, Randomaccess, cloneable , Serializable
ArrayList is a subclass of the list interface, if you want to use the list interface, you must instantiate the list interface through the polymorphism of the object. Here's an example to learn about ArrayList.
Instance one: Add data at the specified location
Import java.util.ArrayList; Import java.util.List; Public class arraylisttest { publicstaticvoid main (string[] args) { List <String> lists=new arraylist<string>(); Lists.add ("Hello"); Added content lists.add (0,"World"); SYSTEM.OUT.PRINTLN (lists); }}
The results of the operation are as follows:
[World, Hello]
Example two: Add content at the specified location
Importjava.util.ArrayList;Importjava.util.List; Public classArraylisttest { Public Static voidMain (string[] args) {List<String> lists=NewArraylist<string>(); List<String> lists1=NewArraylist<string>(); Lists.add ("Hello"); Lists.add ("World"); SYSTEM.OUT.PRINTLN (lists); Lists1.add ("This"); Lists1.add ("Is"); Lists.addall (0, Lists1); Adds a set of content System.out.println (lists) at the specified location; }}
The results of the operation are as follows:
[Hello, World] [This, was, Hello, world]
Example three: delete content (two ways can be used)
Importjava.util.ArrayList;Importjava.util.List; Public classArraylisttest { Public Static voidMain (string[] args) {List<String> lists=NewArraylist<string>(); Lists.add ("Output:"); Lists.add ("Hello"); Lists.add ("World"); SYSTEM.OUT.PRINTLN (lists); Lists.remove (0);//Delete content by numberSystem.out.println (lists); Lists.remove ("Hello");//Content to delete contentSystem.out.println (lists); }}
The results of the operation are as follows:
[Output:, Hello, World] [Hello, World] [World]
Through the above procedure we find that there are ' [] ', still exist ', ' before and after the content, then how to remove them?
Example four: output content in turn
Importjava.util.ArrayList;Importjava.util.List; Public classArraylisttest { Public Static voidMain (string[] args) {List<String> lists=NewArraylist<string>(); Lists.add ("Output:"); Lists.add ("Hello"); Lists.add ("World"); for(intI=0;i<lists.size (); i++) {//the size () method Gets the number of contents in the collectionSystem.out.println (Lists.get (i));//get () method to get the content } }}
The results of the operation are as follows:
Output: Helloworld
This output is unique to the list interface (depending on what is being fetched) and is not on the other interface. This is why the contents of the list interface can be duplicated.
Example Four
Importjava.util.ArrayList;Importjava.util.List; Public classArraylisttest { Public Static voidMain (string[] args) {List<String> lists=NewArraylist<string>(); System.out.println ("Determines whether the collection is empty:" +lists.isempty ()); Lists.add ("Output:"); Lists.add ("Hello"); Lists.add ("World"); System.out.println ("Hello" is in the location: "+lists.indexof (" Hello ")); System.out.println ("Does the World exist:" +lists.contains ("World"))); }}
The output results are as follows:
Determines whether the collection is empty:trueHello is in the position:1 exists world:true
Summarize:
Allow repeating elements in list
Java Learning List Interface