Learning notes of ArrayList class in Java (learning common function usage)

Posted by Kaylub on Tue, 21 Sep 2021 02:24:56 +0200

ArrayList class

The ArrayList class is a special array - a dynamic array. From the System.Collections namespace; By adding and deleting elements, the length of the array can be changed dynamically.


  1. Fast search and fast traversal
  2. Adding and deleting elements are slow

There are many functions in ArrayList. Next, we will analyze and use some of the more commonly used functions.


There are three constructors for ArrayList, as shown below.

// Construct an empty list with an initial capacity of 10
public ArrayList()
// Constructs an empty list with the specified initial capacity
public ArrayList(int initialCapacity)
// Constructs a list containing the elements of the specified collection, arranged in the order that the iterator of the collection returns them
public ArrayList(Collection<? extends E> c)

Add element

When adding elements to the ArrayList container, the add method is generally used. A total of two add methods are provided in the ArrayList, as shown below.

// Adds the specified element to the end of this list
public boolean add(E e)
// Inserts the specified element into the specified location in this list
public void add(int index, E element)

Delete element

When you want to delete an element in the ArrayList container, you usually use the remove method. According to different deletion situations, ArrayList provides two ways to delete, namely, delete according to value matching and delete according to location.

Deleting by location is mainly done by subscript. Because the essence of ArrayList is array, it can be located by subscript 0~size()-1. After specifying the location, you can delete it directly, as shown below.

// Removes the element at the specified location in this list. Move all subsequent elements to the left (reduce their index by 1)
public E remove(int index)

Deleting by value matching is complex, but its essence is judged by the equals() results of the two objects. Therefore, how to judge in the equals() method also determines whether the two objects are the same. By default, it is the address of the comparison. If you want to compare according to other rules, you need to override equals() in the corresponding class Function. For example, if you want to judge whether the name(String) and age(int) in the Student class are the same, you need to override the equals () method, as shown below.

public boolean equals(Object obj) {
    // 1. Judge whether it is the same object
    if(this == obj) {
        return true;
    // 2. Judge whether the object is empty
    if(obj == null) {
        return false;
    // 3. Judge whether it can be converted to Student type (down conversion)
    if(obj instanceof Student) {
        Student s = (Student)obj;
        // 4. Compare attributes
        if(this.name.equals(s.name) && this.age == s.age) {
            return true;
    // 5. Return false if the conditions are not met
    return false;

The definition of deletion method according to whether the objects are the same is as follows.

// Removes the first occurrence of the specified element in this list, if it exists
public boolean remove(Object o)

Get array length

If you want to get the length of the array, or the number of elements in the container, you can get it through the size() function, as defined below.

// Returns the number of elements in this list
public int size()

Modify element

If you want to modify the element data at a position in the ArrayList, you can create a new element and replace the element at the corresponding position with the desired element according to the corresponding position relationship. Here, use the set() function, as defined below.

// Replaces the element at the specified location in this list with the specified element
public E set(int index, E element)

Empty element

Use the clear() function in ArrayList to empty all elements in the container. The prototype of the function is shown below.

// Remove all elements from this list. When this call returns, the list will be empty
public void clear()

Find element

If you want to find an element in the container, you usually use the get() function to get the element. The parameter is subscript, that is, you can get the corresponding element in the container according to the location. The function prototype is as follows.

// Returns the element at the specified location in this list
public E get(int index)

Determine whether an element is included

If you want to judge whether the container contains an element, you can use the contains() function to judge. When judging, you can also judge based on the equals() function. Therefore, the judgment method has the same principle as the previous deletion.

// Returns true if the list contains the specified element
public boolean contains(Object o)

Code example

Simple use of the above functions through some simple code.

public static void main(String[] args) {

        // Create a collection
        ArrayList<Student> arrayList = new ArrayList<>();

        // 1. Add elements
        Student s1 = new Student("Lau Andy", 10);
        Student s2 = new Student("Guo Fucheng", 20);
        Student s3 = new Student("Chao Wei Liang", 30);


        System.out.println("Number of elements:" + arrayList.size());


        // 2. Delete element
//        arrayList.remove(1); / / delete by subscript
//        //Override equals
//        arrayList.remove(new Student("Andy Lau", 10));
//        System.out.println("number of elements:" + arrayList.size());
//        System.out.println(arrayList.toString());
//        System.out.println("================");

        // 3. Traverse elements
        // 3.1 using iterators
        Iterator<Student> it = arrayList.iterator();
        while(it.hasNext()) {
            Student s = it.next();
        // 3.2 using list iterators
        ListIterator<Student> ListIt = arrayList.listIterator();
        while(ListIt.hasNext()) {
            Student s = ListIt.next();
        // Reverse order
        while(ListIt.hasPrevious()) {

        // 4. Judgment
        System.out.println(arrayList.contains(new Student("Lau Andy",10)));

Topics: Java Struts