An article makes you proficient: explanation of java collection (II, ArrayList)

List collection

The main implementation classes are ArrayList and LinkedList, which are the implementation of sequential table and list in data structure respectively. It also includes the implementation classes of stack and Queue. Deque and Queue. (both basic data types and reference data types (objects) can be placed in the array. Only reference data types can be placed in the collection)

List

Features: orderly, not unique (repeatable)

ArrayList

Features: allocate continuous space in memory to realize immutable length.

Advantages: traversal elements and random access elements, high efficiency

Disadvantages: when adding and deleting elements, it requires a lot of forward or backward movement, and the operation is frequent. Inefficient query by content

Common methods of ArrayList
add()
The last element,
Arrays can be automatically boxed: array encapsulation. Common data type - > Packing class
One parameter or two parameters can be used. When one parameter is used, the element is added. When the other parameter is used, the first parameter is index, and the second parameter is element added
Addall (object)

Addall (subscript, object)

Copy and add all the elements of the object, which are added later by default

When there are two parameters, it means that all elements of the object are added after the subscript.

remove()When you enter an object, delete the object. When you enter a pair of subscripts, delete the subscript,
el1.removeAll(el2)
Delete the same element in el2 in el1.
list.retainAll(list2);
Find the same element as list2 in the list
get()
Gets the specified index and returns an object.
set(index,value)Two parameters, the first is the subscript and the second is the modified value
clear()Empty array
contains(value)
Determines whether a value exists and returns a Boolean value
indexOf(value)
Find the subscript index of value
isEmpty()
Determine whether the array is empty.
size()
View the number of elements actually stored in the array. It is different from the length() method of the array
toString()
Traversal array

Note: the following code also has 5 ways to traverse the array

public class Text {
    public static void main(String[] args) {
        //Create an ArrayList collection
        //Method 1
        //ArrayList list = new ArrayList(); 
        //The array can contain both basic data types and reference data types (objects).
        //Only reference data types can be placed in the collection
        //ArrayList < generic > List = new ArrayList < generic > (); A generic type can be understood as an object
        
        //Common creation method: interface oriented programming
        //Polymorphism: benefits when you change your ArrayList() to its subclass LinkedList(), the following code should not be modified
        List list =new ArrayList();
        
        //Add method: add element at the end
        list.add(99);//Arrays can be automatically boxed: array encapsulation. Common data type - > Packing class
        list.add(66);
        list.add(33);
        
        //Add method: the first parameter: index; the second parameter: add element
        //A large number of elements are moved backward at the bottom layer, and array expansion may occur
        list.add(2,100);
        
        //The get method gets the specified index and returns an object.
        int elem=(int) list.get(2);

        //size method to view the number of elements actually stored in the array. Is not the length of the array
        System.out.println(list.size());
        
        //toString method, traversing the array
        System.out.println(list.toString());
        //Method 1, for loop
        for (int i = 0; i <list.size() ; i++) {
           int elem2= (int) list.get(i);
            System.out.println(i+"yes"+elem2);
        }
        //Method 2, for each loop, must use object output
        for (Object elem3:list){
            System.out.println(elem3);
        }

        //Method 3, Iterator (Iterator), iterator() returns the interface of Iterator
        Iterator it=list.iterator();
        while (it.hasNext()){//hasNext(): judge whether there are elements and return Boolean value
            int elem2 =(int) it.next(); //next method: take out the element and return an object.
            System.out.println(elem2);
        }
        
        //Method 4: Lambda expression + process programming (provided by JDK1.8)
        list.forEach((i)-> System.out.println(i));
//      list.forEach(System.out::println);  The second way to write
        
        

Use of generics

Why use generics?

When adding elements to an array, we can how to type data, but when we traverse, we have a problem in forcing type conversion. There are some types that cannot be forced to type. For example, a string cannot be converted to a number.

When we use generics, we fix that only one data type can be stored in the array, which greatly improves the security, and there is no need to use forced type conversion. Make programming easier

How do I use generics?

Yan jinkuanchu

List<Integer> list =new ArrayList();  //Mode 1, 
//  List<Integer> list =new ArrayList<Integer>();   Mode II
//  ArrayList < generic > name = new ArrayList < generic > (); A generic type can be understood as an object

LinkedList

Features: it adopts the storage mode of two-way linked list

Advantages: high efficiency when adding and deleting elements (the premise is that inefficient queries must be performed first. If the insertion and deletion are at the head or tail, the number of queries can be reduced)

Disadvantages: traversal elements and random access elements are inefficient

Tags: Java list set

Posted on Tue, 26 Oct 2021 08:03:43 -0400 by MilesStandish