| English | 意味 |
|---|---|
| ArrayList/əˈreɪ lɪst/ | ArrayList |
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.
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.
add, get, remove
remove(0)後、dogはインデックス0に移動し、サイズは1になります。
ArrayList listの要素数を取得するにはどうすればよいですか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
ArrayListではsize()を使用し、配列では.lengthを使用します。
要素が少なくとも3つある 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)を使用します。
引数1つでadd(x)するとxを...
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
add(x)は末尾に追加し、add(i, x)はインデックスiに挿入します。
[1, 2, 1] から start して、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.