Advanced features of java -- List interface and implementation class

1, Concept of set

1.1 difference between set and array

(1) Length difference: the length of the set is variable, and the length of the array is not variable

(2) Content difference: a collection can store elements of different types, and an array can only store elements of a single type

(3) Element difference: a collection can only store reference type elements. An array can store reference type elements and basic type elements

1.2 concept of set

The collection classes are stored in the java.util package.  
Collection classes store references to objects rather than objects themselves. For the convenience of expression, we call objects in a collection references to objects in a collection.  
There are three main types of sets: set, list and map.

Generally speaking, a collection is a container for storing data. To be exact, it is a container for storing data object references.  

  Java collection classes are mainly derived from the Map interface and the collection interface. Collection consists of two commonly used sub interfaces, namely, the List interface and the set interface. Therefore, it is often said that the collection framework consists of three types of interfaces (Map interface, List interface and set interface).

2, Collection interface

The Collection interface is the top-level interface of a single column Collection and the most basic Collection interface. It can store a group of objects that are not unique and disordered, and defines some general methods.

Add (E) add elements;   clear() clears the element;   Remove (E) remove the element;   size() number of elements;

toArray() set to array;   Contains (E) determines whether the element exists;   isEmpty() determines whether the set is empty;

3, List interface

The common implementation classes of the list interface -- ArrayList and LinkedList

3.1 using ArrayList class to dynamically store data

Data structure: array, but not the same as array. Any type of data can be added to the collection, and the added data will be converted into object type.

Features: fast query, slow addition and deletion. It is mainly used to query traversal data. It is one of the most commonly used sets;

Bottom analysis: the array structure is an ordered sequence of elements. It opens up a continuous space in memory and stores elements in the space. Each space has a number. Through the number, you can quickly find the corresponding elements, so the query is fast; the length of the array is fixed during initialization. If you want to add or delete elements, you must create a new array, copy the elements of the source array, and then the source array Array destruction takes a long time, so adding and deleting is slow.

Give an example to illustrate some common methods of Arraylist class:

public class TestArraylist {
    public static void main(String[] args) {
        ArrayList list=new ArrayList(20);

        list.add(123);//Add an element o at the end of the list, starting at 0
        list.add(1,"I'm a paratrooper");//Adds an element at the specified index o
        list.remove(0);//Remove the element at the hiding index position from the example table

       list.add(10,"88");//Out of range error is reported, and the index exceeds the given data range
//        System.out.println(list);
        System.out.println(list.get(1));//Returns the element at the specified index position. The extracted element is of type Object,
                                          // Mandatory type conversion is required before use
      list.set(2,"The number of subscript positions has changed");//Replace the element at the specified index position with the obj element
//        System.out.println(list);
        System.out.println(list.contains("I am an umbrella"));//Judge whether the specified element exists in the example table o
       System.out.println(list.indexOf("adc"));//Find the subscript of an element in the example table

        //Ordinary for traversal
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//        }
//        Enhanced for traversal
//           for(Object o:list){
//            System.out.println(o);
//        }
        //Iterator traversal
        Iterator itr=list.iterator();
//            Object;


3.2 use LinkedList class to dynamically store data

Data structure: bidirectional linked list;

Features: slow query, fast addition and deletion;

Bottom layer analysis: the linked list is divided into one-way and two-way, which is the difference between one chain and two chains; the extra chain records the order of elements, so the one-way linked list structure is disordered and the two-way linked list structure is orderly; the linked list structure has no index, so the query is slow; the addition and deletion of the linked list only needs to connect the chain or cut off the chain on the original basis, so the addition and deletion is fast.

It contains the methods contained in the ArrayList class, and also provides some methods that can be inserted and deleted at the head or tail of the Linked List class.

public class TestLinkedList {
    public static void main(String[] args) {
        LinkedList list =new LinkedList();

        list.add("handsome guy");
        list.add(0,"I'm a paratrooper");

Tags: Java list

Posted on Tue, 09 Nov 2021 13:10:02 -0500 by examancer