Common methods of List, ArrayList and LinkedList interfaces in Java

Posted by phpparty on Sat, 02 May 2020 08:20:22 +0200

List

    List is an interface

    Differences between ArrayList and LinkedList:

    ArrayList: array implementation
    Features of array implementation: fast query, slow add and delete (refer to LinkList)
    Query: direct use of corner mark query
    Add / delete: all elements after the position of the element to be added need to be moved one bit

    LinkedList: implementation of linked list
    Features of linked list implementation: slow query, fast addition and deletion
    Query: judge whether the query element is close to the head or the tail
          And then find it one by one
    Add / delete: save the address of the element to be inserted without any changes

    When to use ArrayList?
    Use ArrayList if you want to query more
    When to use LinkedList?
    Use LinkedList when adding or deleting more

Common methods in List interface

    public static void fun1() {
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        // Use the add method in List (int index, object obj)
        //The added corner sign is that the position of the following elements cannot exceed the boundary
        list.add(4,"e");
        System.out.println(list);
        //The output is a, b, c, d, e

        //Get the corresponding element through the corner sign
        Object object = list.get(3);
        System.out.println(object);
        //Traverse through get method
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }

        //set method (do not cross boundary)
        list.set(4, "z");
        System.out.println(list);
        //Delete (delete according to the corner sign) returns the deleted element
        Object remove = list.remove(4);
        System.out.println(remove);
        System.out.println(list);
    }

Note when deleting

    public static void fun2() {
        List list = new ArrayList();
        list.add(111);// Automatic packing
        list.add(222);
        list.add(333);
        //Delete 111 elements
        //list.remove(0);
        //When you delete it here, the system does not automatically pack it for you, so you can enter it by number
        ..So the call is a method deleted according to the corner sign, so it is out of bounds
        //If you have to use manual boxing to delete by element
        list.remove(Integrt.valueOff(111));

        System.out.println(list);
    }

Concurrent modification exception occurred

    public static void fun3() {
         /*
         * A d d a b c d to the set
         * If there is b in ergodic set, add a "oh~yes"
         * (Using iterators)
         * 
         */
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        //After adding 4 elements, the length of the collection is 4
        //If iterator traversal is used, the length must be fixed
        //Get iterator
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            if(iterator.next().equals("b")){
                //An exception occurred ConcurrentModificationException
                //Concurrent modification exception
                //Equivalent to modifying the length of the set
                //Note: do not use set operations directly when iterator traverses
                list.add("oh~yes");
            }
        }
        System.out.println(list);
    }   

Solve the abnormal modification of art of war

    public static void fun4() {
    //Idea: let iterators add elements
        List list = new ArrayList();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
        ListIterator listIterator = list.listIterator();
        while(listIterator.hasNext()){
            if(listIterator.next().equals("b")){
                //Let iterators add elements to the collection
                listIteratoe.add("oh~yes");
            }
        }
    System.out.println(list);
    }

vector

    public static void fun5() {
        //Vector jkd1.2 (using arrays to implement has been replaced by ArrayList)
        //Create a Vector set iterator traversal
        Vector vector = new Vector();
        //Add element
        vector.addElement("a");
        vector.addElement("b");
        vector.addElement("c");
        vector.addElement("d");
        System.out.println(vector);
        // Iterator traversal
        // Get iterator in Vector
        Enumeration elements = vector.elements();
        while (elements.hasMoreElements()) {
            System.out.println(elements.nextElement());
        }
    }

Reverse traversal set

    public static void fun6() {
        /*
         * Using the unique iterator in list
         * Reverse traversal set
         */
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        ListIterator listIterator = list.listIterator();
        //If you don't hit forward first 
        //So the pointer only wants the address of the first element in the collection
        //So you need to first hit the forward direction and just think of the tail element
        //Then we can traverse from the end
        while(listIterator.hasnext()){
            System.out.println(listiterator.next());
        }

        //reverse
        //hasPrevious determines whether the previous element has
        while(lisIterator.hasPrevious()){
            System.out.println(listIterator.previous());
        }
    }

ArrayList removing duplicate elements

    public static void fun7() {
        /*
         * Create a collection
         * Add a,a,a,b,b,b,c,c,c
         * Remove duplicates
         */
        ArrayList arrayList = new ArrayList();
        arrayList.add("a");
        arrayList.add("a");
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("b");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("c");
        arrayList.add("c");
        //Create a new collection
        ArrayList arrayList2 = new ArrayList();
        //Traverse the old array and take out each element to determine whether it exists in the new array
        //Get old array iterator
        Iterator iterator = arrayList.iterator();
        while(iterator.hasNext();){
        //Determine whether the elements in the old array exist in the new array
        //Do not add to the new array if there is one
        //Cannot call next method twice
            Object next = iterator.next();
            if(!arrayList2.contains(next)){
                arrayList2.add(next);
            }
        }
        System.out.println(arrayList2);
    }
    Create a new one Student class 
    //Age with name
    //To write a constructor set/get Method override toString Method

        /*
         * Create an ArrayList
         * Save 6 students
         * Remove duplicate students
         */
        ArrayList List = new ArrayList();
        //These six students have different addresses, so they are not the same object
        //If you want to repeat it according to the name and age of the object
        //If the name is the same as the age, the two objects are considered to be duplicate

        List.add(new Student("Little",18));
        List.add(new Student("Little",18));
        List.add(new Student("greatly",17));
        List.add(new Student("greatly",17));
        List.add(new Student("Middle China",16));
        List.add(new Student("Middle China",16));
        //Create a new collection
        ArrayList list2 = new ArrayList();

        Iterator iterator = List.iterator;
        while(iterator.hasNext()){
            Object next = iterator.next();
            //Strong transition student
            Student student = (Student)next;
            //The contains method is used to determine the same address in the system
            //The core of contains method in the system is equals method
            //So you need to override the equals method in the Student class
            if(List2.contains(student)){
                list2.add(student);
            }
        }

        //Print array
        for(Object object : list2){
            Student student = (Student)object;
            System.out.println(student);
        }
    }

    //RewrittenequalsMethod:
        @Override
    public boolean equals(Object obj) {
        //The original system compared the address
        //Now the comparison address can't meet my needs
        //I don't look at the address when I need it. I just look at the name and age
        //The same name and age means the same object

        //Put the Object obj of type Object passed in
        //Convert to Student type
        Student student = (Student)obj;
        //Judge that the name and age are the same
        return this.name.equals(student.getName() &&
        this.age == student.getAge());
    }

LinkedList

    LinkedList linkedList = new LinkedList();
        //Add using the addfirst method
        /*
        linkedList.addFirst("a");
        linkedList.addFirst("b");
        linkedList.addFirst("c");
        linkedList.addFirst("d");
        */
        linkedList.addLast("a");
        linkedList.addLast("b");
        linkedList.addLast("c");
        linkedList.addLast("d");
        System.out.println(linkedList);

        //Get head and tail elements
        System.out.println(linkedList.getFirst());
        System.out.println(linkedList.getLast());

LinkedList simulation stack structure

    LinkedList list = new LinkedList();
    //Add element
    list.addLast("a");
    list.addLast("b");
    list.addLast("c");
    list.addLast("d");
    //Simulate stack out
    while(!list.isEmpty){
        Object str = list.removeLast();
        System.out.println(str);
        System.out.println(list);
    }
    Queue structure and stack structure
    Features of stack structure: first in, then in
    Characteristics of queue structure: first in, first out
                                                                              Day.15