ArrayListのメソッド
導入済み
13:31
データコレクション
Take one photo on your phone. To the computer it is not a picture at all — it is a grid of numbers, one for every pixel, about twelve million of them. Now try…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| ArrayList/əˈreɪ lɪst/ | |
| index/ˈɪndeks/ | A position marker or lookup structure used to locate data within a collection or database. |
このカードは新しいため、翻訳ができるまで英語で表示されます。
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=1794225445)
リストにはサイズと要素型が存在する
- ArrayList は可変長のリファレンスシーケンスを格納します。
java.util.ArrayListをインポートし、ArrayList<String> names = new ArrayList<String>();を宣言します;初期サイズは 0 と等しくなります。 - タイパラメータは参照型なので、
intではなくIntegerを使って整数を表すべきである。size()は格納されている要素をカウントする;初期容量は成長のための空きスペースであり、それだけの要素を作成するわけではない。
正しい範囲内で追加または挿入する
add(x)は要素を追加します;add(i, x)は i の位置にある現在の要素の前に挿入します。挿入の場合、有効なインデックスは 0 から size まで(両端を含む)です。したがって、add(size(), x)も追加として機能します。get(i)は既存の要素を読み取るため、そのインデックスは 0 から size - 1 までです。空のリストには有効な get インデックスはありません;挿入が許可されている位置であっても、get(size())は境界外です。
リストのサイズが 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=1794225445)
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 です。
宣言された引数の型によって選択する
- Java は引数の型に基づいて過負荷を選択します。
int index = 1; list.remove(index);は位置 1 を削除し、Integer value = 1; list.remove(value);は値の削除を選択します。 - String リストは、値
"owl"または位置 0 で削除できます。シフトを追跡する前に意図を明確にしてください。等しい値が見つからない場合は false が返され、リストは変更されません。
配列は a[i] と a.length を使用し、ArrayList は get(i) と size() を使用します。挿入の境界と既存要素の境界を別々に保ち、remove に渡される整数のような引数が常に格納された値を指すとは仮定しないでください。
1つの置換と1つの失敗した削除を確認する
[owl, dog]から始めて、set(1, "fox")は"dog"を返し、[owl, fox]をサイズ 2 のまま残します。その後、remove("cat")は false を返し、何も変更しません。- 空リスト、最初の/最後の有効な位置、存在しない値を確認してください。過負荷とその戻り値、および結果のリストを読み取り、置換、挿入、削除には異なる契約があることを理解してください。
ArrayList はメソッドを通じてアクセスされる可変長配列です。0 から size までをインデックスとして挿入します。既存の位置の読み取り、置き換え、または削除は、0 から size - 1 までの範囲で行います。remove(int) は位置を削除し、remove(Object) は見つかれば最初の等しい値を削除します。
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.
何が問題でしたか?この内容はサイトの運営チームだけが確認します。