java.util.Arraysクラスは、配列を操作するためのユーティリティクラスです。
Arraysの概要
Arraysクラスを使用するとソート、検索、比較などの操作を簡単に行うことができます。
Arraysクラスのメソッドはすべて静的(static)メソッドです。
ArraysのAPI
戻値型 | メソッド | 静的 | 説明 |
---|---|---|---|
List<T> | asList(T… a) | ◯ | 配列から固定サイズのリストを返す |
int | binarySearch(int[] a, int val) | ◯ | 配列要素にvalを検索し、インデックスを返す ※引数にはint以外の型も指定可です。 |
int[] | copyOf(int[] original, int size) | ◯ | 配列をsize分コピーする ※第1引数にはint以外の型も指定可です。 |
boolean | equals(int[] a, int[] a2) | ◯ | 2つ配列の値が同じかどうかを判定する ※引数にはint以外の型も指定可です。 |
void | fill(byte[] a, byte b) | ◯ | byte配列版 |
void | fill(byte[] a, int f, int t, byte b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(short[] a, short b) | ◯ | short配列版 |
void | fill(short[] a, int f, int t, short b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(int[] a, int b) | ◯ | int配列版 |
void | fill(int[] a, int f, int t, int b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(long[] a, long b) | ◯ | long配列版 |
void | fill(long[] a, int f, int t, long b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(float[] a, float b) | ◯ | float配列版 |
void | fill(float[] a, int f, int t, float b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(double[] a, double b) | ◯ | double配列版 |
void | fill(double[] a, int f, int t, double b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(boolean[] a, boolean b) | ◯ | boolean配列版 |
void | fill(boolean[] a, int f, int t, boolean b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(char[] a, char b) | ◯ | char配列版 |
void | fill(char[] a, int f, int t, char b) | ◯ | fからtの間の要素のみ値bをセットする |
void | fill(Object[] a, Object b) | ◯ | Object配列版 |
void | fill(Object[] a, int f, int t, Object b) | ◯ | fからtの間の要素のみ値bをセットする |
void | sort(byte[] a) | ◯ | ソートメソッドbyte配列版 |
void | sort(byte[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(short[] a) | ◯ | ソートメソッドshort配列版 |
void | sort(short[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(int[] a) | ◯ | ソートメソッドint配列版 |
void | sort(int[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(long[] a) | ◯ | ソートメソッドlong配列版 |
void | sort(long[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(float[] a) | ◯ | ソートメソッドfloat配列版 |
void | sort(float[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(double[] a) | ◯ | ソートメソッドdouble配列版 |
void | sort(double[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(boolean[] a) | ◯ | ソートメソッドboolean配列版 |
void | sort(boolean[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(char[] a) | ◯ | ソートメソッドchar配列版 |
void | sort(char[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
void | sort(Object[] a) | ◯ | ソートメソッドObject配列版 |
void | sort(Object[] a, int f, int t) | ◯ | fからtの間の要素のみソートする |
Arraysサンプル
- (配列をList型に変換する)
-
String[] arr = {"A", "B", "C"}; List list = Arrays.asList(arr);
- (配列の全要素に初期値をセットする)
-
String[] arr = new String[5]; //5要素の配列を生成 Arrays.fill(arr, "*"); //全要素に"*"をセットする
- (配列をソートする)
-
int[] arr = {3, 2, 8, 1, 9}; Arrays.sort(arr);
コメント