LoginSignup
0
0

ArrayList LinkedHashSet 基本メソッドまとめ

Last updated at Posted at 2023-12-13

よく使いそうなArrayListクラスのメソッドまとめ
インデックス番号は0から始まります。
LinkedHashSetクラスでも使えるみたいです。

宣言

※右側の<>(※ジェネリックス)の中身は省略できます。

宣言
ArrayList<String> fruits = new ArrayList<>();

要素の追加

ArrayListクラスは、値の重複が許可されます。
LinkedHashSetクラスは、値の重複が許可されません。
LinkedHashSetクラスで同じ要素を追加するコードを記述しても、エラーは出ませんが、実際には同じ要素が追加されません。

ArrayListクラスの場合

add(要素)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");
fruits.add("Apple");
System.out.println(fruits);

/*[Apple, Orange, Banana, Apple]*/

LinkedHashSetクラスの場合

add(要素)
LinkedHashSet<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");
fruits.add("Apple"); /*追加されない*/
System.out.println(fruits);

/*[Apple, Orange, Banana]*/

指定の位置に要素の追加

引数にインデックス番号を追加する

add(インデックス番号,要素)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.add(2, "Strawberry");
 
System.out.println(fruits);
/*[Apple, Orange, Strawberry, Banana]*/

要素の上書き

set(インデックス番号,要素)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.set(0,"Lemon");
System.out.println(fruits);
/*[Lemon, Orange, Banana]*/

要素の取得

get(インデックス番号)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

System.out.println(fruits.get(1));
/*Orange*/

要素のインデックス取得

存在しない要素を引数に指定すると -1 が返ってきます。

indexOf(要素)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

System.out.println(fruits.indexOf("Banana"));
/*2*/
System.out.println(fruits.indexOf("DragonFruit"));
/*-1*/

要素の有無の確認

contains(要素)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

System.out.println(fruits.contains("Apple"));
/*true*/
System.out.println(fruits.contains("DragonFruit"));
/*false*/  

要素数の取得

size()
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

System.out.println(fruits.size());
/*3*/

要素の削除(インデックス番号で指定)

remove(インデックス番号)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.remove(1);

System.out.println(fruits);
/*[Apple, Banana]*/

要素の削除(要素で指定)

remove(要素)
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.remove("Orange");

System.out.println(fruits);
/*[Apple, Banana]*/

全ての要素の削除

clear()
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

fruits.clear();

System.out.println(fruits);
/*[]*/

要素が空っぽかどうかの確認

isEmpty()
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

System.out.println(fruits.isEmpty());
/*false*/

fruits.clear();
System.out.println(fruits.isEmpty());
/*true*/

ArrayListのコピー

コピー
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");

ArrayList<String> fruits2 = new ArrayList<>(fruits);

System.out.println(fruits2);
/*[Apple, Orange, Banana]*/

ArrayList 昇順で並び替え

要 import java.util.Collections

Collections.sort 昇順
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(4);
numbers.add(2);
numbers.add(7);

Collections.sort(numbers);

System.out.println(numbers);
/*[2,4,7]*/

ArrayList 降順で並び替え

要 import java.util.Collections

Collections.sort 降順
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(4);
numbers.add(2);
numbers.add(7);

Collections.sort(numbers,Collections.reverseOrder());

System.out.println(numbers);
/*[7,4,2]*/
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0