Java learns from scratch (set List Interface) and list from scratch
1. List interface List is a sub-interface of Collection, which can save repeated content. The interface is defined as follows: public interface List <E> extends Collection <E> 2. List interface Extension Method
| No. |
Method |
Type |
Description |
| 1 |
Public void add (int index, E element) |
Normal |
Add element at specified position |
| 2 |
Public boolean addAll (int index, Collection <? Extends E> c) |
Normal |
Add a group of elements to a specified position |
| 3 |
E get (int index) |
Normal |
Returns the element at the specified position. |
| 4 |
Public int indexOf (Object o) |
Normal |
Locate the specified Element |
| 5 |
Public int lastIndexOf (Object o) |
Normal |
Search for the position of the specified element from the forward |
| 6 |
Public ListIterator <E> listIterator () |
Normal |
Instantiate the ListIterator Interface |
| 7 |
Public E remove (int index) |
Normal |
Deletes an element at a specified position. |
| 8 |
Public List <E> subList (int fromIndex, int toIndex) |
Normal |
Retrieve the child set in the Set |
| 9 |
Public E set (int index, E element) |
Normal |
Replaces the element at the specified position. |
Iii. Common subclasses of the List interface-ArrayList and List
ArrayList implements variable-length arrays and allocates connection space in the memory. It is highly efficient to traverse and randomly access elements.
The linked list is used for storage. The validity period of inserted or deleted elements is relatively high.
IV,ArrayList
Example
package com.pb.demo1;public class Person { private String name; private int age; public Person() {}public Person(String name, int age) { this.name = name; this.age = age;}public String getName() { return name;}public void setName(String name) { this.name = name;}public int getAge() { return age;}public void setAge(int age) { this.age = age;} }
Package com. pb. demo1; import java. util. arrayList; import java. util. list; public class PersonArrayListTest {public static void main (String [] args) {/** create multiple Person objects and assign values to */Person p1 = new Person ("Zhang San ", 21); Person p2 = new Person ("Li Si", 22); Person p3 = new Person ("Wang Wu", 23 ); // create ArrayList List <Person> personlist = new ArrayList <Person> (); // Add the object personlist for the set. add (p1); personlist. add (p2); personlist. add (p3 ); // The length of the output set: System. out. println ("set length:" + personlist. size (); // traverse the set for loop for (int I = 0; I <personlist. size (); I ++) {System. out. println ("name:" + personlist. get (I ). getName () + "Age:" + personlist. get (I ). getAge ();} // use the index to add Person p4 = new Person ("Zhao ", 24);/** you must know the length before adding an index, if the index to be added is greater than the last subscript + 1, * The compilation is successful, but an error is reported during running. * For example, if the query length is 3, the subscript starts from 0 to 2, in this case, you can use the index to add a value index (0 ~ 3) Yes, but not 4 */personlist. add (0, p4); // length of the output set: System. out. println ("set length:" + personlist. size (); // traverse the set forearch loop for (Person p: personlist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ();} // check whether there are p4 objects in the set. true indicates yes, and false indicates no System. out. println ("Whether the collection contains p4 objects:" + personlist. contains (p4); // The subscript of the p2 element from front to back System. out. println ("subscript of p2 element:" + personlist. indexOf (p2); // The subscript of the p2 element starts from the forward System. out. println ("the subscript of the p2 element is backward:" + personlist. lastIndexOf (p2 ));}}
V. Example of a consumer list
Package com. pb. demo1; import java. util. using list; import java. util. list; public class personatelisttest {public static void main (String [] args) {/** create multiple Person objects and assign values to */Person p1 = new Person ("James ", 21); Person p2 = new Person ("Li Si", 22); Person p3 = new Person ("Wang Wu", 23); Person p4 = new Person ("Zhao Liu ", 24); Person p5 = new Person ("Qian Qi", 25); // create a ticket list using list <Person> plist = new ticket list <Person> (); plist. add (p1); plist. add (p2); plist. add (p3); // set length System. out. println ("set length" + plist. size (); // traverses the set for (Person p: plist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ();} // Add the object plist in the first and last rows. addFirst (p4); plist. addLast (p5); System. out. println ("============= add data ======================== "); // set length: System. out. println ("set length" + plist. size (); // traverses the set for (Person p: plist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ();} // get the element of the specified submark: The subscript must already exist. If it does not exist, an error is returned. out. println ("Get the element of the specified subject:" + plist. get (4 ). getName (); System. out. println ("============= remove by subscript ============================ "); // remove element remove, and remove plist by subscript. remove (0); // set length System. out. println ("set length" + plist. size (); // traverses the set for (Person p: plist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ();} System. out. println ("============ directly remove an object ========================== "); // remove element remove object directly, plist. remove (p2); // set length System. out. println ("set length" + plist. size (); // traverses the set for (Person p: plist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ();} System. out. println ("============== remove the first or last ============================ "); // remove the first or last plist element. removeFirst (); plist. removeLast (); // set length System. out. println ("set length" + plist. size (); // traverses the set for (Person p: plist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ();} System. out. println ("============ remove all ========================== "); // remove all elements. The clear method is plist. clear (); // set length System. out. println ("set length" + plist. size (); // traverses the set for (Person p: plist) {System. out. println ("name:" + p. getName () + "Age:" + p. getAge ());}}}6. the Vector class also has a subclass in the List interface: Vector, which belongs to a saved subclass. From the development history of the JAVA Collection, Vector is a primitive class, this class already exists in JDK 1.0. However, after JAVA 2 (JDK 1.2), the concept of the set framework is emphasized, so many new interfaces (such as List) are defined successively ), however, since most people are used to the Vector class, JAVA designers have made the Vector class implement a List interface, which can be preserved. However, because it is a List subclass, the use of the Vector class is not much different from the previous one.
Package com. pb. demo1; import java. util. vector; public class VectorTest {public static void main (String [] args) {Vector <String> plist = new Vector <String> (); plist. add ("Zhang San"); plist. add ("Li Si"); plist. add ("Wang Wu"); plist. add ("Zhao six"); System. out. println ("Length:" + plist. size (); for (String s: plist) {System. out. println (s );}}}