Justin's Words

Java ArrayList

先来一段代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExample {

public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();

list.add("Item1");
list.add("Item2");
list.add(2, "Item3");
list.add("Item4");

System.out.println("The arraylist contains the following elements: " + list);

int pos = list.indexOf("Item2");
System.out.println("The index of Item2 is: " + pos);

boolean check = list.isEmpty();
System.out.println("Checking if the arraylist is empty: " + check);

int size = list.size();
System.out.println("The size of the list is: " + size);

boolean element = list.contains("Item5");
System.out.println("Checking if the arraylist contains the object Item5: " + element);

String item = list.get(0);
System.out.println("The item is the index 0 is: " + item);

System.out.println("Retrieving items with loop using index and size list");
for (int i = 0; i < list.size(); i++) {
System.out.println("Index: " + i + " - Item: " + list.get(i));
}

System.out.println("Retrieving items using foreach loop");
for (String str : list) {
System.out.println("Item is: " + str);
}

System.out.println("Retrieving items using iterator");
for (Iterator<String> it = list.iterator(); it.hasNext();) {
System.out.println("Item is: " + it.next());
}

list.set(1, "NewItem");
System.out.println("The arraylist after the replacement is: " + list);

list.remove(0);

list.remove("Item3");

System.out.println("The final contents of the arraylist are: " + list);

String[] simpleArray = list.toArray(new String[list.size()]);
System.out.println("The array created after the conversion of our arraylist is: " + Arrays.toString(simpleArray));
}
}

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
The arraylist contains the following elements: [Item1, Item2, Item3, Item4]
The index of Item2 is: 1
Checking if the arraylist is empty: false
The size of the list is: 4
Checking if the arraylist contains the object Item5: false
The item is the index 0 is: Item1
Retrieving items with loop using index and size list
Index: 0 - Item: Item1
Index: 1 - Item: Item2
Index: 2 - Item: Item3
Index: 3 - Item: Item4
Retrieving items using foreach loop
Item is: Item1
Item is: Item2
Item is: Item3
Item is: Item4
Retrieving items using iterator
Item is: Item1
Item is: Item2
Item is: Item3
Item is: Item4
The arraylist after the replacement is: [Item1, NewItem, Item3, Item4]
The final contents of the arraylist are: [NewItem, Item4]
The array created after the conversion of our arraylist is: [NewItem, Item4]

逐一分析

创建一个 ArrayList 空对象,泛型指定列表元素类型为 String

1
ArrayList<String> list = new ArrayList<String>();

ArrayList 空对象添加数据

添加格式:

1
2
boolean add(E element);        // 在列表最后添加元素
void add(int index, E element); // 在指定位置添加元素,从 0 开始

代码示例:

1
2
3
4
list.add("Item1");
list.add("Item2");
list.add(2, "Item3");
list.add("Item4");

获取某元素所在的位置

格式:

1
int indexOf(Object o)

代码示例:

1
int pos = list.indexOf("Item2");

检查列表是否为空

格式:

1
boolean isEntry();

代码示例:

1
int size = list.size();

检查是否包含指定元素

格式:

1
boolean contains(Object o)

代码示例:

1
boolean element = list.contains("Item5");

获取指定位置元素

格式:

1
E get(int index);

index 超出列表范围则抛出异常 IndexOutOfBoundsException,列表长度获取:

1
int size();

代码示例:

1
String item = list.get(0);

循环打印列表元素

代码示例:

1
2
3
for (String str : list) {
System.out.println("Item is: " + str);
}

使用迭代器打印列表元素

格式:

1
Iterator<E> iterator();

代码示例:

1
2
3
for (Iterator<String> it = list.iterator(); it.hasNext();) {
System.out.println("Item is: " + it.next());
}

设置指定位置元素

格式:

1
E set(int index, E element);

代码示例:

1
list.set(1, "NewItem");

移除指定位置元素或符合给定值的元素

格式:

1
2
E remove(int index);
boolean remove(Object element);

代码示例:

1
2
list.remove(0);
list.remove("Item3");

将列表转为数组

格式:

1
2
Object[] toArray();        // 转为对象数组
<T> T[] = toArray(T[] a); // 转为指定类型的数组

代码示例:

1
String[] simpleArray = list.toArray(new String[list.size()]);