Java : ArrayList (リスト) - API使用例
ArrayList (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
ArrayListクラスは、配列 による List の実装です。
とても使い勝手がよく、List で一番使われている実装だと思います。
List の実装としては、ArrayList 以外では LinkedList などがあります。
それぞれの実装の違いなどは下記の記事にて解説しています。
関連記事:List(リスト)の基本
final var list = new ArrayList<String>();
System.out.println(list); // []
list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.get(0)); // aaa
System.out.println(list.get(1)); // bbb
System.out.println(list.get(2)); // ccc
AbstractListで宣言されたフィールド
modCount
「Java API 使用例 : AbstractList」をご参照ください。
コンストラクタ
ArrayList ()
final var list = new ArrayList<String>();
System.out.println(list); // []
list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list); // [aaa, bbb, ccc]
ArrayList (int initialCapacity)
初期容量を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
final var list = new ArrayList<Integer>(10000000);
System.out.println(list); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
final var endTime = System.nanoTime();
// 0.084715 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var list = new ArrayList<Integer>(1);
System.out.println(list); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
final var endTime = System.nanoTime();
// 0.160947 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
ArrayList (Collection<? extends E> c)
final var c = List.of("aaa", "bbb", "ccc");
System.out.println(c); // [aaa, bbb, ccc]
final var list = new ArrayList<>(c);
System.out.println(list); // [aaa, bbb, ccc]
メソッド
void add (int index, E element)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
list.add(0, "A");
System.out.println(list); // [A, a, b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
list.add(1, "B");
System.out.println(list); // [a, B, b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
list.add(2, "C");
System.out.println(list); // [a, b, C, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
list.add(3, "D");
System.out.println(list); // [a, b, c, D]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
//list.add(4, "D"); // IndexOutOfBoundsException
}
boolean add (E e)
final var list = new ArrayList<String>();
System.out.println(list); // []
System.out.println(list.add("a")); // true
System.out.println(list); // [a]
System.out.println(list.add("b")); // true
System.out.println(list); // [a, b]
System.out.println(list.add("c")); // true
System.out.println(list); // [a, b, c]
boolean addAll (int index, Collection<? extends E> c)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(0, List.of("A1", "A2"))); // true
System.out.println(list); // [A1, A2, a, b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(1, List.of("B1", "B2", "B3"))); // true
System.out.println(list); // [a, B1, B2, B3, b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(2, List.of("C1", "C2", "C3", "C4"))); // true
System.out.println(list); // [a, b, C1, C2, C3, C4, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.addAll(3, List.of())); // false
System.out.println(list); // [a, b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
//list.addAll(4, List.of("E")); // IndexOutOfBoundsException
}
final var src = List.of("a", "b", "c");
System.out.println(src);// [a, b, c]
final var list = new ArrayList<>(src);
System.out.println(list.addAll(3, Set.of("D1", "D2", "D3"))); // true
System.out.println(list); // [a, b, c, D3, D2, D1]
boolean addAll (Collection<? extends E> c)
final var list = new ArrayList<String>();
System.out.println(list.addAll(List.of("a1"))); // true
System.out.println(list); // [a1]
System.out.println(list.addAll(List.of("b1", "b2"))); // true
System.out.println(list); // [a1, b1, b2]
System.out.println(list.addAll(List.of("c1", "c2", "c3"))); // true
System.out.println(list); // [a1, b1, b2, c1, c2, c3]
System.out.println(list.addAll(List.of())); // false
System.out.println(list); // [a1, b1, b2, c1, c2, c3]
final var list = new ArrayList<String>();
System.out.println(list); // []
System.out.println(list.addAll(Set.of("c1", "c2", "c3"))); // true
System.out.println(list); // [c3, c2, c1]
void addFirst (E element)
final var list = new ArrayList<String>();
list.addFirst("aaa");
System.out.println(list); // [aaa]
list.addFirst("bbb");
System.out.println(list); // [bbb, aaa]
list.addFirst("ccc");
System.out.println(list); // [ccc, bbb, aaa]
void addLast (E element)
final var list = new ArrayList<String>();
list.addLast("aaa");
System.out.println(list); // [aaa]
list.addLast("bbb");
System.out.println(list); // [aaa, bbb]
list.addLast("ccc");
System.out.println(list); // [aaa, bbb, ccc]
void clear ()
final var list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list); // [a, b, c]
list.clear();
System.out.println(list); // []
Object clone ()
final var list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list); // [a, b, c]
final var cloned = list.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.ArrayList
boolean contains (Object o)
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
System.out.println(list.contains("")); // false
System.out.println(list.contains("a")); // true
System.out.println(list.contains("b")); // true
System.out.println(list.contains("c")); // true
System.out.println(list.contains("abc")); // false
void ensureCapacity (int minCapacity)
容量を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
final var list = new ArrayList<Integer>();
list.ensureCapacity(10000000);
System.out.println(list); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
final var endTime = System.nanoTime();
// 0.085877 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var list = new ArrayList<Integer>();
System.out.println(list); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
final var endTime = System.nanoTime();
// 0.174232 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
boolean equals (Object o)
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
System.out.println(list.equals(List.of("a"))); // false
System.out.println(list.equals(List.of("a", "b"))); // false
System.out.println(list.equals(List.of("a", "b", "c"))); // true
System.out.println(list.equals(List.of("c", "b", "a"))); // false
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
final var arrayList = new ArrayList<>(list);
System.out.println(arrayList); // [a, b, c]
System.out.println(list.equals(arrayList)); // true
final var linkedList = new LinkedList<>(list);
System.out.println(linkedList); // [a, b, c]
System.out.println(list.equals(linkedList)); // true
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
final var set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(list.equals(set)); // false
void forEach (Consumer<? super E> action)
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println("-- forEach --");
list.forEach(element -> {
System.out.println(element);
});
// 結果
// ↓
//-- forEach --
//aaa
//bbb
//ccc
E get (int index)
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
//list.get(-1); // IndexOutOfBoundsException
System.out.println(list.get(0)); // a
System.out.println(list.get(1)); // b
System.out.println(list.get(2)); // c
//list.get(3); // IndexOutOfBoundsException
final var list = new ArrayList<Integer>();
Collections.addAll(list, 10, 20, 30, 40);
System.out.println(list); // [10, 20, 30, 40]
System.out.println(list.get(0)); // 10
System.out.println(list.get(1)); // 20
System.out.println(list.get(2)); // 30
System.out.println(list.get(3)); // 40
E getFirst ()
final var list = new ArrayList<String>();
list.addLast("aaa");
System.out.println(list); // [aaa]
System.out.println(list.getFirst()); // aaa
list.addLast("bbb");
System.out.println(list); // [aaa, bbb]
System.out.println(list.getFirst()); // aaa
list.addLast("ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.getFirst()); // aaa
final var list = new ArrayList<String>();
System.out.println(list); // []
try {
final var ret = list.getFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
E getLast ()
final var list = new ArrayList<String>();
list.addLast("aaa");
System.out.println(list); // [aaa]
System.out.println(list.getLast()); // aaa
list.addLast("bbb");
System.out.println(list); // [aaa, bbb]
System.out.println(list.getLast()); // bbb
list.addLast("ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.getLast()); // ccc
final var list = new ArrayList<String>();
System.out.println(list); // []
try {
final var ret = list.getLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
int hashCode ()
final var list1 = new ArrayList<String>();
Collections.addAll(list1, "a", "b", "c");
System.out.println(list1.hashCode()); // 126145
final var list2 = new ArrayList<String>();
Collections.addAll(list2, "A", "B", "C");
System.out.println(list2.hashCode()); // 94369
final var list3 = new ArrayList<Integer>();
Collections.addAll(list3, 1, 2, 3);
System.out.println(list3.hashCode()); // 30817
int indexOf (Object o)
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]
System.out.println(list.indexOf("")); // -1
System.out.println(list.indexOf("a")); // 0
System.out.println(list.indexOf("b")); // 1
System.out.println(list.indexOf("c")); // 2
System.out.println(list.indexOf("d")); // -1
System.out.println(list.indexOf("abc")); // -1
boolean isEmpty ()
final var list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list); // [a, b, c]
System.out.println(list.isEmpty()); // false
list.clear();
System.out.println(list); // []
System.out.println(list.isEmpty()); // true
Iterator<E> iterator ()
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.iterator();
iterator.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
int lastIndexOf (Object o)
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]
System.out.println(list.lastIndexOf("")); // -1
System.out.println(list.lastIndexOf("a")); // 3
System.out.println(list.lastIndexOf("b")); // 4
System.out.println(list.lastIndexOf("c")); // 5
System.out.println(list.lastIndexOf("d")); // -1
System.out.println(list.lastIndexOf("abc")); // -1
ListIterator<E> listIterator ()
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var listIterator = list.listIterator();
listIterator.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
ListIterator<E> listIterator (int index)
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
{
final var ret = list.listIterator(0);
ret.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
}
{
final var ret = list.listIterator(1);
ret.forEachRemaining(System.out::println);
// 結果
// ↓
//bbb
//ccc
}
{
final var ret = list.listIterator(2);
ret.forEachRemaining(System.out::println);
// 結果
// ↓
//ccc
}
E remove (int index)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.remove(0)); // a
System.out.println(list); // [b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.remove(1)); // b
System.out.println(list); // [a, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.remove(2)); // c
System.out.println(list); // [a, b]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
//list.remove(3); // IndexOutOfBoundsException
}
boolean remove (Object o)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.remove("")); // false
System.out.println(list); // [a, b, a, b, A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.remove("a")); // true
System.out.println(list); // [b, a, b, A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.remove("b")); // true
System.out.println(list); // [a, a, b, A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.remove("A")); // true
System.out.println(list); // [a, b, a, b, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.remove("B")); // true
System.out.println(list); // [a, b, a, b, A]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.remove("C")); // false
System.out.println(list); // [a, b, a, b, A, B]
}
boolean removeAll (Collection<?> c)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.<String>of())); // false
System.out.println(list); // [a, b, a, b, A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.of("a"))); // true
System.out.println(list); // [b, b, A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.of("a", "b"))); // true
System.out.println(list); // [A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.of("b", "a"))); // true
System.out.println(list); // [A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.of("A"))); // true
System.out.println(list); // [a, b, a, b, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.of("X", "Y", "Z"))); // false
System.out.println(list); // [a, b, a, b, A, B]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.removeAll(List.of("A", "X", "Y", "Z"))); // true
System.out.println(list); // [a, b, a, b, B]
}
E removeFirst ()
final var list = new ArrayList<String>();
list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.removeFirst()); // aaa
System.out.println(list); // [bbb, ccc]
System.out.println(list.removeFirst()); // bbb
System.out.println(list); // [ccc]
System.out.println(list.removeFirst()); // ccc
System.out.println(list); // []
final var list = new ArrayList<String>();
System.out.println(list); // []
try {
final var ret = list.removeFirst();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
boolean removeIf (Predicate<? super E> filter)
final var list = new ArrayList<String>();
list.add("aaa");
list.add("BBB");
list.add("ccc");
list.add("DDD");
System.out.println(list); // [aaa, BBB, ccc, DDD]
final var ret = list.removeIf(s -> {
return s.equals(s.toUpperCase());
});
System.out.println(ret); // true
System.out.println(list); // [aaa, ccc]
final var list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
System.out.println(list); // [aaa, bbb]
final var ret = list.removeIf(s -> s.equals(s.toUpperCase()));
System.out.println(ret); // false
System.out.println(list); // [aaa, bbb]
E removeLast ()
final var list = new ArrayList<String>();
list.addLast("aaa");
list.addLast("bbb");
list.addLast("ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.removeLast()); // ccc
System.out.println(list); // [aaa, bbb]
System.out.println(list.removeLast()); // bbb
System.out.println(list); // [aaa]
System.out.println(list.removeLast()); // aaa
System.out.println(list); // []
final var list = new ArrayList<String>();
System.out.println(list); // []
try {
final var ret = list.removeLast();
} catch (NoSuchElementException e) {
System.out.println("NoSuchElementException!");
}
// 結果
// ↓
//NoSuchElementException!
protected void removeRange (int fromIndex, int toIndex)
@SuppressWarnings("serial")
class MyList extends ArrayList<String> {
MyList(Collection<String> c) {
super(c);
}
@Override
public void removeRange(int fromIndex, int toIndex) {
super.removeRange(fromIndex, toIndex);
}
}
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final var list = new MyList(src);
System.out.println(list); // [a, b, c]
list.removeRange(0, 1);
System.out.println(list); // [b, c]
}
{
final var list = new MyList(src);
System.out.println(list); // [a, b, c]
list.removeRange(0, 2);
System.out.println(list); // [c]
}
{
final var list = new MyList(src);
System.out.println(list); // [a, b, c]
list.removeRange(0, 3);
System.out.println(list); // []
}
{
final var list = new MyList(src);
System.out.println(list); // [a, b, c]
list.removeRange(1, 3);
System.out.println(list); // [a]
}
{
final var list = new MyList(src);
System.out.println(list); // [a, b, c]
list.removeRange(2, 3);
System.out.println(list); // [a, b]
}
{
final var list = new MyList(src);
System.out.println(list); // [a, b, c]
list.removeRange(3, 3);
System.out.println(list); // [a, b, c]
}
boolean retainAll (Collection<?> c)
final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.<String>of())); // true
System.out.println(list); // []
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.of("a", "b"))); // true
System.out.println(list); // [a, b, a, b]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.of("b", "a"))); // true
System.out.println(list); // [a, b, a, b]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.of("A"))); // true
System.out.println(list); // [A]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.of("X", "Y", "Z"))); // true
System.out.println(list); // []
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.of("A", "X", "Y", "Z"))); // true
System.out.println(list); // [A]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, a, b, A, B]
System.out.println(list.retainAll(List.of("a", "b", "A", "B"))); // false
System.out.println(list); // [a, b, a, b, A, B]
}
E set (int index, E element)
final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.set(0, "A")); // a
System.out.println(list); // [A, b, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.set(1, "B")); // b
System.out.println(list); // [a, B, c]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
System.out.println(list.set(2, "C")); // c
System.out.println(list); // [a, b, C]
}
{
final var list = new ArrayList<>(src);
System.out.println(list); // [a, b, c]
//list.set(3, "D"); // IndexOutOfBoundsException
}
int size ()
final var list = new ArrayList<String>();
System.out.println(list); // []
System.out.println(list.size()); // 0
list.add("a");
System.out.println(list); // [a]
System.out.println(list.size()); // 1
list.add("b");
list.add("c");
list.add("d");
System.out.println(list); // [a, b, c, d]
System.out.println(list.size()); // 4
Spliterator<E> spliterator ()
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var spliterator = list.spliterator();
spliterator.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
List<E> subList (int fromIndex, int toIndex)
final var list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
System.out.println(list); // [a, b, c, d]
System.out.println(list.subList(0, 0)); // []
System.out.println(list.subList(0, 1)); // [a]
System.out.println(list.subList(0, 2)); // [a, b]
System.out.println(list.subList(0, 3)); // [a, b, c]
System.out.println(list.subList(0, 4)); // [a, b, c, d]
//list.subList(0, 5); // IndexOutOfBoundsException
final var subList = list.subList(1, 3);
System.out.println(subList); // [b, c]
System.out.println(list); // [a, b, c, d]
subList.set(0, "X");
System.out.println(subList); // [X, c]
System.out.println(list); // [a, X, c, d]
subList.add("Y");
System.out.println(subList); // [X, c, Y]
System.out.println(list); // [a, X, c, Y, d]
subList.remove("c");
System.out.println(subList); // [X, Y]
System.out.println(list); // [a, X, Y, d]
Object[] toArray ()
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
final Object[] array = list.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]
<T> T[] toArray (T[] a)
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
final String[] array = list.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [a, b, c]
final var list = new ArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]
{
final String[] array = new String[3];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = list.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
final String[] array = new String[5];
System.out.println(Arrays.toString(array)); // [null, null, null, null, null]
final var ret = list.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
final String[] array = new String[1];
System.out.println(Arrays.toString(array)); // [null]
final var ret = list.toArray(array);
System.out.println(Arrays.toString(array)); // [null]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
void trimToSize ()
final var list = new ArrayList<String>(10000000);
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var runtime = Runtime.getRuntime();
runtime.gc();
final var before = runtime.totalMemory() - runtime.freeMemory();
System.out.println(before); // 45640832
list.trimToSize();
runtime.gc();
final var after = runtime.totalMemory() - runtime.freeMemory();
System.out.println(after); // 4742320
final var diff = before - after;
System.out.println(diff); // 40898512
AbstractListで宣言されたメソッド
equals, hashCode
「Java API 使用例 : AbstractList」をご参照ください。
AbstractCollectionで宣言されたメソッド
containsAll, toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
parallelStream, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Listで宣言されたメソッド
containsAll, replaceAll, reversed, sort
「Java API 使用例 : List」をご参照ください。
関連記事
- List(リスト)の基本
- Map(マップ)の基本
- Set(セット)の基本
- List の初期化方法いろいろ
- Map の初期化方法いろいろ
- Set の初期化方法いろいろ
- 配列 vs. List
- 配列からListへの変換
- List から配列への変換
- API 使用例
- Collection (コレクション)
- Collections (コレクション操作)
- Comparable
- Comparator
- Iterator
- List (リスト)
- Map (マップ)
- Map.Entry (キーと値のペア)
- Queue (キュー)
- AbstractQueue
- ArrayBlockingQueue (ブロッキング・配列キュー)
- ArrayDeque (両端キュー)
- BlockingDeque (ブロッキング・両端キュー)
- BlockingQueue (ブロッキング・キュー)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- Deque (両端キュー)
- LinkedBlockingDeque (ブロッキング・リンク両端キュー)
- LinkedBlockingQueue (ブロッキング・リンクキュー)
- LinkedList (二重リンク・リスト)
- PriorityBlockingQueue (ブロッキング・優先度付きキュー)
- PriorityQueue (優先度付きキュー)
- RandomAccess
- Set (セット)
- Spliterator