Usage of common Collections: ArrayList and LinkedList

Author: Sannian

Collection

  |--List: index, duplicate elements allowed
  |--Set: no index, duplicate elements are not allowed
  • [java.util.List]

    ArrayList: the underlying data structure is an array, and the physical address of the array is continuous. You can quickly locate the address of a specific element through the location, so the query efficiency is high
    However, when adding and deleting elements, operations such as creating arrays, copying elements and moving elements may be involved, which is time-consuming, so the efficiency of adding and deleting is low

    LinkedList: the underlying layer is a two-way circular list connected by nodes, with discontinuous physical addresses,
    It is necessary to rely on the head and tail nodes to address continuously to find the target location. The process of addressing is time-consuming, so the efficiency of query is low;
    When adding or deleting, you only need to find the target location to disconnect and connect nodes, so the efficiency of adding or deleting is high;

1.ArrayList

import java.util.ArrayList;
import java.util.Iterator;

/**
 * ArrayList
 * @author yujie
 *
 */
public class SN1_ArrayList {
    public static void main(String[] args) {
        // 1. Construct an empty list with an initial capacity of 10 -- ArrayList
        ArrayList<Object> arrayList = new ArrayList<>();
        // 2. Add data in the linked list (it can be a number, a string, or an object)
        for (int i = 0; i < 10; i++) {
            arrayList.add("Small" + i);
        }
        // 3. Display list content
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(arrayList.get(i) + " ");
        }
        System.out.println();
        // 4. Delete small 3
        arrayList.remove(3);
        // 5. Add A small A behind the small 2
        arrayList.add(3, "Small A");
        // 6. Change the name of small A to small B
        arrayList.set(3, "Small B");
        // 7. Return the array containing all elements in this list
        Object[] array = arrayList.toArray();
        // 8. Return the number of elements in this list
        System.out.println("Number of elements: " + arrayList.size());
        // 9. Judge whether the list contains small 8
        System.out.println(arrayList.contains("Small 8"));
        /**
         * iterator
         */
        Iterator<Object> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.print(next+" ");
        }
        System.out.println();

        // Note: to delete a linked list, you cannot use the normal for loop method. You need to use an iterator, as follows: delete the small 1:
        Iterator<Object> it = arrayList.iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if("Small 1".equals(next)) it.remove();
        }
        // The display method can also be used as follows
        for (Object s : arrayList) {
            System.out.print(s+" ");
        }

    }
}

demonstration:

2.LinkedList

import java.util.LinkedList;

/**
 * LinkedList
 * @author yujie
 *
 */
public class SN2_LinkedList {
    public static void main(String[] args) {
        // 1. Construct an empty list with an initial capacity of 10 -- ArrayList
        LinkedList<Object> linkedList = new LinkedList<>();
        // 2. Add data in the linked list (it can be a number, a string, or an object)
        for (int i = 0; i < 10; i++) {
            linkedList.add("Small" + i);
        }
        // 3. Display list content
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i) + " ");
        }
        System.out.println();
        // 4. Delete small 3
        linkedList.remove(3);
        // 5. Add A small A behind the small 2
        linkedList.add(3, "Small A");
        // 6. Change the name of small A to small B
        linkedList.set(3, "Small B");
        // 7. Return the array containing all elements in this list
        Object[] array = linkedList.toArray();
        // 8. Return the number of elements in this list
        System.out.println("Number of elements: " + linkedList.size());
        // 9. Get the first and last elements of the list
        System.out.println("first:" + linkedList.getFirst() + " the last one:" + linkedList.getLast());
        // 10. Judge whether the list contains small 8
        System.out.println(linkedList.contains("Small 8"));
        //11. Remove and return the first element of this list.
        System.out.println(linkedList.removeFirst());
        System.out.println(linkedList.getFirst());

    }
}

demonstration:

Tags: Java

Posted on Mon, 04 May 2020 22:55:41 -0700 by lovely