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;.
成長可能なリスト
- 配列は 固定サイズ である。一度作成すると、長さは変更不能である。
ArrayListは、プログラム実行中に 成長 したり 縮小 したりできるリストである。- ファイルの先頭で インポート する必要がある:
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>は整数を保持する。- 新しいリストは initially 空 であり、サイズは
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.
必要な5つのメソッド
list.size()— リスト内のアイテムの数。list.add(x)—xをリストの 最後 に追加する。list.get(i)— 位置iのアイテムを取得する(最初の位置は 0)。list.set(i, x)— 位置iのアイテムをxに置き換える。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は オブジェクト しか保持できず、素型であるintは保持できない。- そのため
ArrayList<Integer>を使用する。Integerはintのオブジェクト形式である。 - Java は
intとIntegerの間で自動的に変換を行う。これを オートボクシング という。 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.
2つの遍历方法
- インデックス付き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) を完成させなさい。インデックス i の値を get で読み取り、set を使って2倍した値をインデックス i に格納しなさい。何も返さない(void)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。