Java SeriesArrayList

ArrayList

  • Add element
  • access element
  • Modify elements
  • Delete element
  • Calculate size
  • Iterate over a list of arrays
  • Other reference types
  • ArrayList sorting
  • Java ArrayList method
  • Series of articles
  • Series of articles
  • version record


introduction
The ArrayList class is an array that can be dynamically modified. The difference from an ordinary array is that it has no fixed size limit and we can add or delete elements. ArrayList inherits AbstractList and implements the List interface.

The ArrayList class is located in the java.util package and needs to be introduced before use. The syntax format is as follows:

import java.util.ArrayList; //Introduce the ArrayList class

ArrayList<E> objectName =new ArrayList<>(); // Initialization
  • E: Generic data type, used to set the data type of objectName, which can only be reference data types.
  • objectName: object name.
  • ArrayList is an array queue that provides related functions such as adding, deleting, modifying, and traversing.

Add element

The ArrayList class provides many useful methods. To add elements to an ArrayList, you can use the add() method:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        System.out.println(sites);
    }
}

For the above example, the execution output is:

[Google, Runoob, Taobao, Weibo]

Access elements

To access elements in an ArrayList, use the get() method:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        System.out.println(sites.get(1)); // Access the second element
    }
}

Note: Array index values start from 0.

For the above example, the execution output is:

Runoob

Modify elements

If you want to modify the elements in the ArrayList, you can use the set() method:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        sites.set(2, "Wiki"); // The first parameter is the index position, the second is the value to be modified
        System.out.println(sites);
    }
}

For the above example, the execution output is:

[Google, Runoob, Wiki, Weibo]

Delete element

If you want to delete elements in ArrayList, you can use the remove() method:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        sites.remove(3); // Remove the fourth element
        System.out.println(sites);
    }
}

For the above example, the execution output is:

[Google, Runoob, Taobao]

Calculate size

If you want to count the number of elements in an ArrayList, you can use the size() method:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        System.out.println(sites.size());
    }
}

For the above example, the execution output is:

4

Iterate over array list

We can use for to iterate over the elements in an array list:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        for (int i = 0; i < sites.size(); i + + ) {<!-- -->
            System.out.println(sites.get(i));
        }
    }
}

For the above example, the execution output is:

Google
Runoob
Taobao
Weibo

You can also use for-each to iterate over elements:

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        for (String i : sites) {<!-- -->
            System.out.println(i);
        }
    }
}

For the above example, the execution output is:

Google
Runoob
Taobao
Weibo

Other reference types

The elements in ArrayList are actually objects. In the above example, the array list elements are all of type String.

If we want to store other types, but only reference data types, then we need to use a wrapper class of the basic type.

The packaging class table corresponding to the basic types is as follows:

In addition, BigInteger and BigDecimal are used for high-precision operations. BigInteger supports arbitrary-precision integers and is also a reference type, but they do not have corresponding basic types.

ArrayList<Integer> li=new ArrayList<>(); // Store integer elements
ArrayList<Character> li=new ArrayList<>(); // Store character elements

The following example uses an ArrayList to store numbers (using the Integer type):

Example

import java.util.ArrayList;

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<Integer> myNumbers = new ArrayList<Integer>();
        myNumbers.add(10);
        myNumbers.add(15);
        myNumbers.add(20);
        myNumbers.add(25);
        for (int i : myNumbers) {<!-- -->
            System.out.println(i);
        }
    }
}

For the above example, the execution output is:

10
15
20
25

ArrayList sorting

The Collections class is also a very useful class located in the java.util package. It provides the sort() method to sort a list of characters or numbers.

The following example sorts letters:

Example

import java.util.ArrayList;
import java.util.Collections; //Introduce Collections class

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Taobao");
        sites.add("Wiki");
        sites.add("Runoob");
        sites.add("Weibo");
        sites.add("Google");
        Collections.sort(sites); // Alphabetical sorting
        for (String i : sites) {<!-- -->
            System.out.println(i);
        }
    }
}

For the above example, the execution output is:

Google
Runoob
Taobao
Weibo
Wiki

The following example sorts numbers:

Example

import java.util.ArrayList;
import java.util.Collections; //Introduce Collections class

public class RunoobTest {<!-- -->
    public static void main(String[] args) {<!-- -->
        ArrayList<Integer> myNumbers = new ArrayList<Integer>();
        myNumbers.add(33);
        myNumbers.add(15);
        myNumbers.add(20);
        myNumbers.add(34);
        myNumbers.add(8);
        myNumbers.add(12);

        Collections.sort(myNumbers); //Number sorting

        for (int i : myNumbers) {<!-- -->
            System.out.println(i);
        }
    }
}

For the above example, the execution output is:

8
12
15
20
33
34

Java ArrayList method

The list of common Java ArrayList methods is as follows:

Series of articles

Series of articles

Content Address link
JAVA series Java Introduction
JAVA Series Java Basics


================================================== =======================


If you are interested in this series of articles, please continue to follow the blogger’s updates. The blogger will continue to output high-quality content

?

Bloggers need everyone’s support. Your support is the inexhaustible motivation for my creation



?

~ Like and collect + follow~< /font>



================================================== =======================

Version record

  • 2023-10-18 First Edition
syntaxbug.com © 2021 All Rights Reserved.