ArrayList: a list that grows · ArrayList:可以增长的列表
A list that can grow
- An array has a fixed size. Once you make it, the length cannot change.
- An
ArrayListis a list that can grow and shrink while the program runs. - You must import it at the top of the file:
import java.util.ArrayList;.
一个可以增长的列表
- 数组(array)的大小是固定的。一旦创建,长度就不能改变。
ArrayList是一种列表,它在程序运行时可以增长和缩小。- 你必须在文件顶部导入(import)它:
import java.util.ArrayList;。
Making an ArrayList
- Write the type of the items inside
<...>. This is the AP CSA way. ArrayList<String>holds text.ArrayList<Integer>holds whole numbers.- A new list starts empty, with size
0.
创建一个 ArrayList
- 把元素的类型写在
<...>里面。这是 AP CSA 的写法。 ArrayList<String>存放文本。ArrayList<Integer>存放整数。- 新建的列表一开始是空的,大小为
0。
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
System.out.println(names.size()); // 0
names.add("Ann");
names.add("Bo");
System.out.println(names.size()); // 2
System.out.println(names); // [Ann, Bo]
}
}
The five methods you need
list.size()— how many items are in the list.list.add(x)— putxat the end of the list.list.get(i)— the item at positioni(the first position is 0).list.set(i, x)— replace the item at positioniwithx.list.remove(i)— take out the item at positioni; later items shift left.
你需要的五个方法
list.size()—— 列表里有多少个元素。list.add(x)—— 把x放到列表的末尾。list.get(i)—— 位置i上的元素(第一个位置是 0)。list.set(i, x)—— 用x替换位置i上的元素。list.remove(i)—— 取出位置i上的元素;后面的元素会向左移动。
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruit = new ArrayList<String>();
fruit.add("apple");
fruit.add("pear");
fruit.add("plum");
System.out.println(fruit.get(0)); // apple
fruit.set(1, "grape"); // pear -> grape
fruit.remove(2); // take out "plum"
System.out.println(fruit); // [apple, grape]
}
}
Numbers need Integer
- An
ArrayListcan only hold objects, not the simple typeint. - So we write
ArrayList<Integer>.Integeris the object form ofint. - Java changes between
intandIntegerfor you. This is called autoboxing. - You can
add(7)anint, andget(0)gives back a value you can use asint.
数字需要 Integer
ArrayList只能存放对象(object),不能存放简单类型int。- 所以我们写
ArrayList<Integer>。Integer是int的对象形式。 - Java 会替你在
int和Integer之间转换。这叫作自动装箱(autoboxing)。 - 你可以
add(7)一个int,而get(0)会返回一个可以当作int使用的值。
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(10); // int 10 is boxed into Integer
nums.add(20);
int first = nums.get(0); // unboxed back to int
System.out.println(first + nums.get(1)); // 30
}
}
Two ways to traverse
- An index for-loop uses positions
0tosize() - 1. Use it when you need the index. - An enhanced for-loop (
for (int x : list)) reads each item in turn. Use it when you only need the values. - Both visit every item once, in order.
两种遍历方式
- 下标 for 循环使用位置
0到size() - 1。当你需要下标时用它。 - 增强 for 循环(
for (int x : list))依次读取每个元素。当你只需要值时用它。 - 两种方式都会按顺序访问每个元素一次。
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(5);
nums.add(8);
// index for-loop
for (int i = 0; i < nums.size(); i++) {
System.out.println("at " + i + ": " + nums.get(i));
}
// enhanced for-loop
int total = 0;
for (int x : nums) {
total = total + x;
}
System.out.println("total " + total); // total 16
}
}
Common mistakes
ArrayListuses.add,.get(i),.size()— not[].- Store objects: use
ArrayList<Integer>, notArrayList<int>.
常见错误
ArrayList用.add、.get(i)、.size()——不是[]。- 存对象:用
ArrayList<Integer>,不是ArrayList<int>。
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
现在轮到你
- 每个任务都已经填好了类的骨架 —— 把你的代码写在 main 里面,或者补全给出的方法。
- 按运行来编译并运行,然后按检查答案。
- 你的代码在服务器上编译并运行,所以第一次运行也很快。
An ArrayList grows · ArrayList 会增长
Unlike an array, an ArrayList changes size as you add/remove. · 和数组不同,ArrayList 会随着添加/删除而改变大小。
Complete sumList(ArrayList<Integer> a) so it returns the total · 总 of all numbers in the list. An empty list totals 0. Traverse with a loop. · 完成 sumList(ArrayList<Integer> a),让它返回列表里所有数字的总和。空列表的总和是 0。用循环来遍历。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete countUp(int n) so it makes a new · 新的 ArrayList<Integer>, adds the numbers 1, 2, ..., n in order, and returns it. If n is 0, return an empty list. · 完成 countUp(int n),让它创建一个新的 ArrayList<Integer>,按顺序加入数字 1, 2, ..., n,然后返回它。如果 n 是 0,就返回一个空列表。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete doubleAt(ArrayList<Integer> a, int i). Read the value at index i with get, then use set to store two times that value back at index i. Return nothing (void). · 完成 doubleAt(ArrayList<Integer> a, int i)。用 get 读取下标 i 上的值,然后用 set 把那个值的两倍存回下标 i。不返回任何东西(void)。
Click Run to see the output here. · 点击“运行”查看此处输出。