List collection (including LinkedList collection and Vector collection)

In the previous article, I learned the use of the Collection interface, and then I learned the subclasses of the Collection interface, java.util.List and java.util.Map collections.

The java.util.List interface inherits extends from the Collection interface. It is an important branch of the single-column collection. Objects that implement the List interface are habitually called the List collection. Duplicate elements are allowed in the List collection . All elements are Stored in a linear manner, the specified element in the collection can be accessed by index in the program. In addition, another feature of the List collection is that the elements are ordered , that is, the order in which the elements are stored and taken out are the same.

List interface features:

1. It is an ordered collection of element access. For example, the order of storing the elements is 11, 22, 33. Then in the set, the storage of the elements is done in the order of 11, 22, 33.

2. It is a collection with an index, and the elements in the collection can be accurately manipulated through the index (the same as the index of the array).

3. There can be repeated elements in the collection, and the equals method of the elements is used to compare whether they are repeated elements.

Tips: I have learned the java.util.ArrayList class, a subclass of the List interface before. The methods of this class are all defined in List.

List common methods:

Note: When operating the index, be sure to prevent the index out of bounds exception. (Do not exceed the index range)

public void add(int index, E element): Add the specified element to the specified position in the collection.

public E get(int index): Returns the element at the specified position in the collection.

public E remove(int index): Remove the element at the specified position in the list, and return the removed element.

public E set(int index, E element): Replace the element at the specified position in the set with the specified element, and the return value is the element before the update .

public static void main(String[] args) {
    // 多态写法
    List<String> list = new ArrayList<>();
   // 按顺序添加元素
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    // 打印集合
    System.out.println(list); // [aaa, bbb, ccc] 重写了toString方法

    // 按索引添加元素
    // public void add(int index,E element):将指定的元素,添加到该集合中的指定位置上。
    // 在bbb和ccc之间,添加ddd
    // 注意不是替换,是先将该索引位置的元素及后面的元素后移,而后插入
    list.add(2,"ddd");
    System.out.println(list); // [aaa, bbb, ddd, ccc]

    // public E remove(int index):移除列表中指定位置的元素,返回的是被移除的元素。
    // 移除ddd元素,注意,此处后续元素也是有前移动作
    String str = list.remove(2);
    System.out.println("被移除的元素:" + str); // 被移除的元素:ddd
    System.out.println(list); // [aaa, bbb, ccc]

    // public E set(int index,E element):
    // 用指定元素替换集合中指定位置的元素,返回值是更新前的元素。
    // 将aaa替换为AAA
    String str1 = list.set(0,"AAA");
    System.out.println("被替换的元素:" + str1); // 被替换的元素:aaa
    System.out.println(list); // [AAA, bbb, ccc]

    // public E get(int index):返回集合中指定位置的元素。
    // List集合遍历有三种方式
    // 1.普通的遍历
    System.out.println("普通for循环遍历");
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
    // 2.for each遍历
    System.out.println("for each循环遍历");
    for (String str2 : list) {
        System.out.println(str2);
    }

    // 3.迭代器遍历
    System.out.println("迭代器遍历");
    Iterator<String> iterator = list.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next());
    }
}

List subclass (implementation class)

1. ArrayList collection (previously introduced), the corresponding data structure is: dynamic array

Collection ArrayList

ArrayList has high query efficiency because of the continuous storage of addresses. It only needs to provide an index. The time complexity is O(1), but additions and deletions need to move data, and the time complexity reaches O(n). When a large number of additions and deletions are required in the program When data, the ArrayList data structure is not suitable for use.

2.LinkedList

The structure of java.util.LinkedList collection data storage is a linked list structure. A collection that facilitates the addition and deletion of elements.

LinkedList is a doubly linked list.

Features:

1. The bottom layer is a linked list structure: the query is slow, and the addition and deletion is fast.

2. It contains a large number of methods to operate the beginning and end.

Note: The unique method of the LinkedList collection is used, and polymorphism cannot be used!

Common methods:

public void addFirst(E e): Add the specified element to the beginning of this list.

public void addLast(E e): Add the specified element to the end of this list.

public E getFirst(): Returns the first element of this list.

public E getLast(): Returns the last element of this list.

public E removeFirst(): Remove and return the first element of this list.

public E removeLast(): Remove and return the last element of this list.

public E pop(): Pop an element from the stack represented by this list.

public void push(E e): Push the element into the stack represented by this list.

public boolean isEmpty(): If the list contains no elements, it returns true.

/*
public E removeFirst():移除并返回此列表的第一个元素。
public E removeLast():移除并返回此列表的最后一个元素。
public E pop():从此列表所表示的堆栈处弹出一个元素。
 */
private static void show03() {
    LinkedList<String> linkedList = new LinkedList<>();
    linkedList.add("aaa");
    linkedList.add("bbb");
    linkedList.add("ccc");

    // public E removeFirst():移除并返回此列表的第一个元素。
    String removeFirst = linkedList.removeFirst();
    System.out.println("被移除的第一个元素:" + removeFirst); // 被移除的第一个元素:aaa
    System.out.println(linkedList); // [bbb, ccc]

    // public E removeLast():移除并返回此列表的最后一个元素。
    String removeLast = linkedList.removeLast();
    System.out.println("被移除的最后一个元素:" + removeLast); // 被移除的最后一个元素:ccc
    System.out.println(linkedList); // [bbb]

    // public E pop():从此列表所表示的堆栈处弹出一个元素。
    // 此方法相当于removeFirst方法
    String pop = linkedList.pop();
    System.out.println("从栈顶弹出的元素:" + pop); // bbb
    System.out.println(linkedList); //
}

// public E getFirst():返回此列表的第一个元素。
// public E getLast():返回此列表的最后一个元素。
private static void show02() {
    LinkedList<String> linkedList = new LinkedList<>();
    linkedList.add("aaa");
    linkedList.add("bbb");
    linkedList.add("ccc");
    if(!linkedList.isEmpty()) {
        String first = linkedList.getFirst();
        String last = linkedList.getLast();
        System.out.println("第一个元素:" + first); // aaa
        System.out.println("最后一个元素:" + last); // cc
    }
    // 清空
    linkedList.clear();
    System.out.println(linkedList.size()); // 0
}

/*
public void addFirst(E e):将指定元素添加到此列表的开头。
public void addLast(E e):将指定元素添加到此列表的结尾。
public void push(E e):将元素推入此列表所表示的堆栈。
 */
private static void show01() {
    // 创建LinkedList集合对象,不能使用多态
    LinkedList<String> linkedList = new LinkedList<>();
    // 使用add方法往集合中添加元素
    linkedList.add("aaa");
    linkedList.add("bbb");
    linkedList.add("ccc");
    // 使用addFirst方法往集合开头添加元素
    linkedList.addFirst("AAA");
    System.out.println(linkedList); // [AAA, aaa, bbb, ccc]

    // public void push(E e):将元素推入此列表所表示的堆栈。
    // 等效于addFirst方法
    linkedList.push("111");
    System.out.println(linkedList); // [111, AAA, aaa, bbb, ccc]

    // public void addLast(E e):将指定元素添加到此列表的结尾。
    // 此方法等效于add方法
    linkedList.addLast("DDD");
    System.out.println(linkedList); // [111, AAA, aaa, bbb, ccc, DDD]
}

The Vector class can implement a growing array of objects (the bottom layer is a dynamic array like ArrayList), and Vector is synchronous (single-threaded) and slow.

Guess you like

Origin blog.csdn.net/wardo_l/article/details/113984583