Java ArrayList collection quick start

Posted by pernest on Fri, 28 Jan 2022 20:32:16 +0100

1. Assemble

A collection is a container similar to an array, but its storage capacity is variable.

  • Array features:
    • After the array definition is completed, the type is determined and the length is fixed.
    • It is suitable for the scenario of determining the number and type of data.
    • Existing problems: when the number is uncertain and the data needs to be added or deleted, the array is not appropriate.
  • Features of the collection:
    • The size of the collection is not fixed. It can change dynamically after startup, and the type can also be selected as not fixed.
    • Collection is very suitable for business scenarios where the number of elements is uncertain and addition and deletion operations are required.
    • Collection provides many rich and easy-to-use functions, while the function of array is very single.

2. ArrayList set

ArrayList is a collection that supports indexing.

  • Characteristics of ArrayList set:
    • The bottom layer is realized by array, and the length can be changed.
  • Use of generics:
    • Used to constrain the type of array elements in the collection.

3. Common methods of ArrayList class

3.1 construction method

constructor explain
public ArrayList()Create an empty collection object

3.2 method of adding elements

Method nameexplain
public boolean add(E e)Appends the specified element to the end of this collection
public void add(int index,E element)Inserts the specified element at the specified location in this collection
import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args) {
        // 1. Create ArrayList collection object
        ArrayList list = new ArrayList();

        // 2. Add data
        list.add("Labrador");
        list.add("Not much");
        list.add(10);
        list.add(20.1);
        list.add('cat');
        list.add(true);
        list.add(false);
        System.out.println(list);

        // 3. Insert elements into the specified index
        list.add(1, "Alaska");
        System.out.println(list);
    }
}
// Printout
[Labrador, Not much, 10, 20.1, cat, true, false]
[Labrador, Alaska, Not much, 10, 20.1, cat, true, false]

4. Generics

  • summary:

    • For example, ArrayList is a generic type, which can restrict the collection object to operate only a certain data type at the compilation stage.
  • give an example:

    • ArrayList: this collection can only manipulate elements of string type.
    • ArrayList: this collection can only operate on elements of integer type.
  • be careful:

    • Only reference types can be stored in the collection, and basic data types are not supported.
  • Code demonstration:

    import java.util.ArrayList;
    
    public class ArrayListDemo {
        public static void main(String[] args) {
            // 1. Create ArrayList collection object
    //      ArrayList<String> list = new ArrayList<String>();
    //							        From jdk1 Starting from 7, the type declaration after generic type can not be written
            
            ArrayList<String> list = new ArrayList<>();
            
            // 2. Add data
            list.add("Labrador");
            list.add("Not much");
            list.add(10);      // report errors
            list.add(20.1);    // report errors
            list.add(true);    // report errors
            System.out.println(list);
    
            // 3. Insert elements into the specified index
            list.add(1, 99);    // report errors
            System.out.println(list);
        }
    }
    

5. Common API s of ArrayList set

Method nameexplain
public boolean remove(Object o)Delete the specified element and return whether the deletion is successful
public E remove(int index)Deletes the element at the specified index and returns the deleted element
public E set(int index,E element)Modify the element at the specified index and return the modified element
public E get(int index)Returns the element at the specified index
public int size()Returns the number of elements in the collection
  • Code demonstration:

    import java.util.ArrayList;
    
    public class ArrayListDemo {
        public static void main(String[] args) {
            // Create an ArrayList collection object
            ArrayList<String> list = new ArrayList<>();
    
            // Add data
            list.add("Labrador");
            list.add("German Shepherd");
            list.add("Labrador");
            list.add("Alaska");
            list.add("Chinese garden cat");
            list.add("Golden hair");
    
    //      Public e get (int index) returns the element at the specified index
            String e = list.get(3);
            System.out.println(e);
    
    //      public int size() gets the size of the collection (number of elements)
            System.out.println(list.size());
    
            // Traversal set
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
    
    //      Public e remove (int index) deletes the element at the specified index and returns the deleted element
            System.out.println(list);
            String e1 = list.remove(5);  // Delete the golden hair and use e1 to connect the return value
            System.out.println(e1);
            System.out.println(list);
    
    //      public boolean remove(Object o) deletes the specified element and returns whether the deletion was successful
            System.out.println(list.remove("Alaska"));
            System.out.println(list);
            System.out.println(list.remove("Labrador")); // When there are the same elements in the collection, only the first element in these same elements will be deleted
            System.out.println(list);
    
    //      Public e set (int index, e element) modifies the element at the specified index and returns the modified element
            String e2 = list.set(2, "Chinese pastoral dog");  // "Chinese garden cat" - > "Chinese garden dog"
            System.out.println(e2);
            System.out.println(list);
        }
    }   
    
    // Output result:
    D:\Software\Java\jdk\bin\java.exe ...
    Alaska
    6
     Labrador
     German Shepherd
     Labrador
     Alaska
     Chinese garden cat
     Golden hair
    [Labrador, German Shepherd, Labrador, Alaska, Chinese garden cat, Golden hair]
    Golden hair
    [Labrador, German Shepherd, Labrador, Alaska, Chinese garden cat]
    true
    [Labrador, German Shepherd, Labrador, Chinese garden cat]
    true
    [German Shepherd, Labrador, Chinese garden cat]
    Chinese garden cat
    [German Shepherd, Labrador, Chinese pastoral dog]
    
    Process finished with exit code 0
    

Topics: Java Back-end