| English | 释义 | 拼音 |
|---|---|---|
| ArrayList/əˈreɪ lɪst/ | 动态数组 | dòng tài shù zǔ |
Insertion changes the later indices
- An ArrayList starts
[cat, dog].add(1, "owl")makes[cat, owl, dog], thenremove(0)makes[owl, dog].get(1)now returns"dog". - Trace the whole list after each operation: insertion shifts later elements right, while removal shifts them left. An index identifies a position, not a permanent identity for an element.
![An insertion and a removal change later indices: start [cat, dog]; add(1, "owl") gives [cat, owl, dog], shifting dog from index 1 to 2; remove(0) gives [owl, dog], shifting both left](/learn-media/ap_computer_science_a/assets/0408-index-shifts.png?v=1794647927)
A list has a size and an element type
- An ArrayList 动态数组 stores a resizable sequence of references. Import
java.util.ArrayListand declareArrayList<String> names = new ArrayList<String>();; size initially equals 0. - The type parameter is a reference type, so use
Integerfor whole numbers rather thanint.size()counts stored elements; an initial capacity is space available for growth and does not create that many elements.
Append or insert within the right bounds
add(x)appends an element;add(i, x)inserts before the current element at i. For insertion, valid indices run from 0 through size inclusive, soadd(size(), x)appends too.get(i)reads an existing element, so its indices run from 0 through size minus 1. An empty list has no valid get index;get(size())is out of bounds even though insertion there is allowed.
对于大小为2的列表,add(2, value)是允许的,但get(2)会越界。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
插入操作允许在size位置进行;而get操作要求索引必须严格小于size且已存在。
Replacement and removal return different results
set(i, x)replaces an existing element and returns the old value without changing size.remove(int i)removes the element at that position, returns it, and decreases size by one.- The overloaded
remove(Object value)instead removes the first equal value and returns whether a match was removed. In anArrayList<Integer>,remove(1)means index 1; useremove(Integer.valueOf(1))to remove the value 1.
import java.util.ArrayList;
import java.util.Arrays;
public class RemoveChoice {
public static void main(String[] args) {
ArrayList<Integer> byIndex = new ArrayList<>(Arrays.asList(1, 2, 1));
ArrayList<Integer> byValue = new ArrayList<>(Arrays.asList(1, 2, 1));
int removed = byIndex.remove(1);
boolean found = byValue.remove(Integer.valueOf(1));
System.out.println(removed + " " + byIndex);
System.out.println(found + " " + byValue);
}
}
The two outputs are 2 [1, 1] and true [2, 1]. Each list loses one element, but a different one.
![Same argument value, different declared type: on the list [1, 2, 1], remove(1) uses an int index and removes index 1, giving [1, 1] and returning 2; remove(Integer.valueOf(1)) removes the first value equal to 1, giving [2, 1] and returning true](/learn-media/ap_computer_science_a/assets/0408-remove-overload.png?v=1794647927)
add、get 与 remove
remove(0) 后,dog 移到下标 0,大小降到 1。
如何获取 ArrayList list 中的元素个数?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
ArrayList 用 size();数组用 .length。
对于一个至少有三个元素的 ArrayList list,如何访问索引为 2 的元素?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
ArrayList 用 get(i),而非 [i]。
对 {cat, dog} 执行 a.remove(0) 后,a.get(0) 是什么?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
remove(0) 删除 cat;dog 移到下标 0。
你可以用方括号访问 ArrayList 元素,如 list[0]。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
ArrayList 用方法(get/set),而非 [] 语法。
带一个参数的 add(x) 把 x 添加到……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
add(x) 追加;add(i, x) 在下标 i 处插入。
从 [1, 2, 1] 开始,对 int 参数调用 remove(1)。返回什么值?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
int重载移除了索引1,其元素为2。
Choose by the declared argument type
- Java selects the overload using the argument type.
int index = 1; list.remove(index);removes position 1;Integer value = 1; list.remove(value);selects value removal. - A String list can remove
"owl"by value or index 0 by position. Decide which you mean before tracing shifts; finding no equal value returns false and leaves the list unchanged.
Arrays use a[i] and a.length; ArrayList uses get(i) and size(). Keep insertion bounds separate from existing-element bounds, and do not assume that an integer-looking argument to remove always means a stored value.
Check one replacement and one failed removal
- Starting from
[owl, dog],set(1, "fox")returns"dog"and leaves[owl, fox]with size 2.remove("cat")then returns false and changes nothing. - Check empty lists, the first/last valid positions, and a value that is absent. Read the overload and its return value as well as the resulting list; replacing, inserting and deleting have different contracts.
ArrayList is a resizable sequence accessed through methods. Insert at 0 through size; read, replace or remove an existing position at 0 through size minus 1. remove(int) deletes a position; remove(Object) deletes the first equal value if found.
After the stated insertion and removal, type the value returned by get(0).
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The final list is [owl, dog], with owl at index 0.