Java ArrayList
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. It needs to be introduced before use. The syntax format is as follows:
import java.util.ArrayList; // 引入 ArrayList 类
ArrayList<E> objectName =new ArrayList<>(); // 初始化
- E : Generic data type, used to set the data type of objectName, it can only be a reference data type .
- objectName : The name of the object.
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 the ArrayList, you can use the add() method:
Instance
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);
}
}
In the above example, the execution output result is:
[Google, Runoob, Taobao, Weibo]
Access element
To access the elements in the ArrayList, you can use the get() method:
Instance
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)); // 访问第二个元素
}
}
Note : The index value of the array starts from 0.
In the above example, the execution output result is:
Runoob
Modify elements
If you want to modify the elements in the ArrayList, you can use the set() method:
Instance
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"); // 第一个参数为索引位置,第二个为要修改的值
System.out.println(sites);
}
}
In the above example, the execution output result is:
[Google, Runoob, Wiki, Weibo]
Delete element
If you want to delete the elements in the ArrayList, you can use the remove() method:
Instance
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); // 删除第四个元素
System.out.println(sites);
}
}
In the above example, the execution output result is:
[Google, Runoob, Taobao]
Calculate the size
If you want to count the number of elements in the ArrayList, you can use the size() method:
Instance
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());
}
}
In the above example, the execution output result is:
4
Iterate over an array list
We can use for to iterate the elements in the array list:
Instance
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));
}
}
}
In the above example, the execution output result is:
Google
Runoob
Taobao
Weibo
You can also use for-each to iterate the elements:
Instance
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);
}
}
}
In the above example, the execution output result is:
Google
Runoob
Taobao
Weibo
Other reference types
The elements in ArrayList are actually objects. In the above example, the elements of the array list are all strings of String type.
If we want to store other types, but only for reference data types, then we need to use the basic type of packaging class.
The packaging class table corresponding to the basic type is as follows:
basic type | Reference type |
---|---|
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
In addition, BigInteger and BigDecimal are used for high-precision operations. BigInteger supports arbitrary-precision integers and is also a reference type, but they have no corresponding basic types.
ArrayList<Integer> li=new Arraylist<>(); // 存放整数元素
ArrayList<Character> li=new Arraylist<>(); // 存放字符元素
The following example uses ArrayList to store numbers (using Integer type):
Instance
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);
}
}
}
In the above example, the execution output result is:
10
15
20
25
ArrayList sort
The Collections class is also a very useful class. It is located in the java.util package. The sort() method provided can sort a list of characters or numbers.
The following example sorts the letters:
Instance
import java.util.ArrayList;
import java.util.Collections; // 引入 Collections 类
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); // 字母排序
for (String i : sites) {
System.out.println(i);
}
}
}
In the above example, the execution output result is:
Google
Runoob
Taobao
Weibo
Wiki
The following example sorts the numbers:
Instance
import java.util.ArrayList;
import java.util.Collections; // 引入 Collections 类
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); // 数字排序
for (int i : myNumbers) {
System.out.println(i);
}
}
}
In the above example, the execution output result is:
8
12
15
20
33
34
bers = new ArrayList<Integer>();
myNumbers.add(33);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(34);
myNumbers.add(8);
myNumbers.add(12);
Collections.sort(myNumbers); // 数字排序
for (int i : myNumbers) {
System.out.println(i);
}
}
}
In the above example, the execution output result is:
8
12
15
20
33
34