Java data structure, list collection, ArrayList collection, LinkedList collection, Vector collection

data structure:

The commonly used structures of data storage are: stack, queue, array, linked list, red-black tree.

Stack: stack, also known as stack, is a linear table with limited operations. Its limitation is that it only allows insertion and deletion operations at one end of the target, and does not allow operations such as addition, search, and deletion at any other position. Simply put, With the collection of this structure, the access to elements has the characteristics of forward and backward, and the entry and exit of the stack are both the top positions of the stack.
insert image description here
Queue: queue, referred to as team, is like a stack, and it is also a linear table with limited operations. The limitation is that it only allows insertion at one end of the table and deletion at the other end of the table. Simply put, the collection using this structure has the characteristics of first-in first-out, and the entrance and exit of the queue occupy one side.
insert image description here
Array: an ordered sequence of elements, an array is a continuous space opened up in memory, and elements are stored in this space, like a row of rental houses, with 100 rooms, these 100 rooms are sequentially numbered , you can quickly find elements by numbering. Its characteristics: you can quickly find elements by index, but adding and deleting elements is slow; (adding elements at specified index positions: you need to create a new array, store the specified new elements at the specified index position, in Copy the elements of the original array to the position corresponding to the index of the new array according to the index)
insert image description here
Linked list: linked list, composed of a series of node nodes, each element in the linked list is called a node, and the node can be dynamically generated at runtime. A node consists of two parts, one is the data field for storing data elements, and the other is the pointer field for storing the address of the next node. The linked list that is often said has a one-way linked list and a two-way linked list. The characteristics of the one-way linked list: multiple nodes They are connected through addresses, finding elements is slow, and adding and deleting elements is fast (to find an element, you need to search for the specified element backward through the connected nodes in turn, and to add an element, you only need to modify the address to connect to the next element. Red-black tree: binary
insert image description here
tree , binary tree, is an ordered tree with no more than 2 nodes per node, the top node is called the root node, and the nodes on both sides are called the left subtree and right subtree, its characteristics: the query speed is very fast.
insert image description here
list gather:

import java.util.ArrayList;
import java.util.List;

// List集合继承了Collection接口,具有以下特点:存和取元素是有序的、有索引且包含了带索引的方法,允许存储重复的元素
public class ListClass {
    
    
    public static void main(String[] args) {
    
    
        // 创建一个list集合:并使用它特有的方法:
        List<String> list = new ArrayList<String>();

        // 1.add()向集合中添加元素:add方法传入一个参数时表示给list末尾追加元素;当有两个参数时,参数1表示要追加元素的索引位置,参数二表示追加的元素
        list.add("a");
        list.add("a");
        list.add("bc");
        System.out.println(list); // [a, a, bc]

        list.add(1,"b");
        System.out.println(list); // [a, b, a, bc]

        // 2.remove()移除指定索引位置的元素,并返回被移除的元素,接收一个参数时即元素索引
        String removedElement = list.remove(0);
        System.out.println(removedElement); // a
        System.out.println(list); // [b, a, bc]

        // 3.set()用某个元素替换集合中指定位置的元素,并将之前的被替换的元素返回,接收两个参数时,第一个参数是要替换元素的索引位置,第二个参数表示要替换的新元素
        String setElement = list.set(1, "f");
        System.out.println(setElement); // a
        System.out.println(list); // [b, f, bc]

        // 4.get()获取指定索引位置的元素,传入一个参数表示索引
        String getElement = list.get(1);
        System.out.println(getElement); // f

        // 扩展:遍历集合可以使用普通的for-i,通过get(i)拿到每一个元素外,还可以使用迭代器,除此之外,也可以使用加强版for(类似JavaScript中的for in,只需将in改为冒号,且这里变量表示某个元素,而JavaScript中变量表示索引),如:
        for (String item : list) {
    
    
            System.out.println(item); // 分别打印每一个元素
        };
    }
}

The underlying principle of the ArrayList collection:

ArrayList is an implementation class of the List collection. The bottom layer is an array. The characteristic length of the array remains unchanged. When using methods such as add, the actual method is to create a new array with a length greater than the original by 1, and then copy the old array to the new one. It is not difficult to understand the disadvantages of adding and deleting ArrayList, and because the index of the elements in the array is continuous, the query speed is very fast.

LinkedList collection:

This collection is a linked list implementation of the List interface, and this implementation class is multi-threaded, that is, asynchronous. Pay special attention to the fact that this implementation class cannot use polymorphism to create objects.

import java.util.LinkedList;

// LinkedList是List接口的一个实现类,底层是一个链表结构,查询慢,增删快,并且该实现类有很多特有的方法来操作元素,因为是特有的,因此不能使用多态的方式来使用这些特有的方法。
public class LinkedListClass {
    
    
    public static void main(String[] args) {
    
    
        // 因为LinkedList不能使用多态的方式创建对象,因此我们使用普通的方式创建对象并使用其特有的方法:
        LinkedList<String> lt = new LinkedList<String>();

        // 使用非特有方法add()向结合中添加默认元素:
        lt.add("a");
        lt.add("b");
        lt.add("c");
        System.out.println(lt); // [a, b, c]

        // 下面是特有方法:
        // 1.addFirst()方法向集合的最前面添加元素:
        lt.addFirst("F");
        System.out.println(lt); // [F, a, b, c]

        // 2.push()向集合最前面添加元素,等效于addFirst(),和javascript中push方法有个天壤之别,因为js中push方法是在数组后面追加元素:
        lt.push("H");
        System.out.println(lt); // [H, F, a, b, c]

        // 3.addLast()向集合的最后面添加元素,和add()没多大区别:
        lt.addLast("V");
        System.out.println(lt); // [H, F, a, b, c, V]

        // 4.getFirst()获取并返回第一个元素:空集合不能使用此方法,否则会报异常
        String firstItem = lt.getFirst();
        System.out.println(firstItem); // H

        // 5.getLast()获取并返回最后一个元素:空集合不能使用此方法,否则会报异常
        String lastItem = lt.getLast();
        System.out.println(lastItem); // V

        // 6.removeFirst()移除并返回集合的第一个元素:
        String reFiItem = lt.removeFirst();
        System.out.println(reFiItem); // H
        System.out.println(lt); // [F, a, b, c, V]

        // 7.pop()移除返回集合第一个元素,和removeFirst()类似:
        String pFiItem = lt.pop();
        System.out.println(pFiItem); // F
        System.out.println(lt); // [a, b, c, V]

        // 8.removeLast()移除并返回集合中最后一个元素:
        String reLastItem = lt.removeLast();
        System.out.println(reLastItem); // V
        System.out.println(lt); // [a, b, c]

        // 9.clear()清空集合中的元素:
        lt.clear();
        System.out.println(lt); // []
    }
}

Vector set:

The Vector class can implement a growable array of objects, single-threaded. The previous old api was replaced by ArrayList.

Tips: The pictures and other materials in this article come from the Internet. If there is any infringement, please send an email to the mailbox: [email protected] to contact the author to delete it.
Author: sea of ​​bitterness

Guess you like

Origin blog.csdn.net/weixin_46758988/article/details/128123558