目錄
public static List asList(T... a):將多個參數轉換爲ArrayList
public static int binarySearch(byte[] a, byte key):查詢元素第一次出現的位置
char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public static int binarySearch(byte[] a, int fromIndex, int toIndex,byte key):指定範圍key第一次出現的位置
char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public static char[] copyOf(char[] original, int newLength):複製數組
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public static boolean[] copyOfRange(boolean[] original, int from, int to) :複製數組某一段
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public static boolean equals(byte[] a, byte[] a2):長度相等,元素相等
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public static void fill(boolean[] a, boolean val):用val替換數組中所有的值
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public static void fill(int[] a, int fromIndex, int toIndex, int val):用val替換數組指定範圍的值
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public static int hashCode(int a[]):計算hashCode方法
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public static void parallelSort(byte[] a):對數組進行併發排序
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public static void parallelPrefix(int[] array, IntBinaryOperator op):修改的值是1---length-1,位置0的元素不會被修改
double[]、int[]、long[]、T[]均有此方法
double[]、int[]、long[]、T[]均有此方法
public static void parallelSetAll(int[] array, IntUnaryOperator generator):併發修改
double[]、int[]、long[]、T[]均有此方法
public static void setAll(int[] array, IntUnaryOperator generator):單線程修改修改
double[]、int[]、long[]、T[]均有此方法
public static void sort(int[] a):升序排序
char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public static IntStream stream(int[] array)
public static String toString(boolean[] a):格式化輸出
char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
常用方法總結
public static <T> List<T> asList(T... a):將多個參數轉換爲ArrayList
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
System.out.println(list);
List<String> list1 = Arrays.asList("1","2","3");
System.out.println(list1);
List<Long> list2 = Arrays.asList(1L,2L);
System.out.println(list2);
}
}
結果:
[1, 2, 3, 4, 5]
[1, 2, 3]
[1, 2]
public static int binarySearch(byte[] a, byte key):查詢元素第一次出現的位置
char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
byte[] a = {'a','b','c','a'};
//底層採用二分法,所以要先對數組進行排序
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a, (byte) 'a'));
}
}
結果:
0
public static int binarySearch(byte[] a, int fromIndex, int toIndex,byte key):指定範圍key第一次出現的位置
char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
//底層採用二分法,所以要先對數組進行排序
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a,1,a.length, (byte) 'a'));
}
}
結果:
1
public static char[] copyOf(char[] original, int newLength):複製數組
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
//底層採用二分法,所以要先對數組進行排序
byte[] bytes = Arrays.copyOf(a, a.length);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
}
}
結果:
97
98
99
97
public static boolean[] copyOfRange(boolean[] original, int from, int to) :複製數組某一段
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
//底層採用二分法,所以要先對數組進行排序
byte[] bytes = Arrays.copyOfRange(a,2, a.length);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
}
}
結果:
99
97
public static boolean equals(byte[] a, byte[] a2):長度相等,元素相等
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
byte[] a = {(byte)'a',(byte)'b',(byte)'c',(byte)'a'};
byte[] bytes = Arrays.copyOf(a, a.length);
//長度相等,每個元素相等,返回true
System.out.println(Arrays.equals(a,bytes));
}
}
結果:
true
public static void fill(boolean[] a, boolean val):用val替換數組中所有的值
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
Arrays.fill(a, 5);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
結果:
5
5
5
5
public static void fill(int[] a, int fromIndex, int toIndex, int val):用val替換數組指定範圍的值
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
Arrays.fill(a, 0,2,5);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
結果
5
5
3
4
public static int hashCode(int a[]):計算hashCode方法
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
System.out.println(Arrays.hashCode(a));
}
}
結果:
955331
public static void parallelSort(byte[] a):對數組進行併發排序
boolean[]、char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {1,8,51,13,46,11,22};
Arrays.parallelSort(a);
System.out.println(Arrays.toString(a));
}
}
結果:
[1, 8, 11, 13, 22, 46, 51]
public static void parallelPrefix(int[] array, IntBinaryOperator op):修改的值是1---length-1,位置0的元素不會被修改
double[]、int[]、long[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
System.out.println(Arrays.toString(a));
Arrays.parallelPrefix(a, new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
//left當前元素前一個索引的值(2,8,51,13,46,11)即:0---length-2
//right:當前索引的元素(8,51,13,46,11,22)即:1---length-1
System.out.println(left+":"+right);
//修改的值是1---length-1,位置0的元素不會被修改
return 2*right;
}
});
System.out.println(Arrays.toString(a));
}
}
結果:除了元素1,其他元素都被擴大了兩倍
[2, 8, 51, 13, 46, 11, 22]
2:8
16:51
102:13
26:46
92:11
22:22
[2, 16, 102, 26, 92, 22, 44]
public static void parallelPrefix(int[] array, int fromIndex,int toIndex, IntBinaryOperator op):修改指定範圍內的值
double[]、int[]、long[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
System.out.println(Arrays.toString(a));
Arrays.parallelPrefix(a,1,4, new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
//left當前元素前一個索引的值(2,8,51,13,46,11)即:0---length-2
//right:當前索引的元素(8,51,13,46,11,22)即:1---length-1
System.out.println(left+":"+right);
//修改的值是1---length-1,位置0的元素不會被修改
return 2*right;
}
});
System.out.println(Arrays.toString(a));
}
}
結果:(1,4)內元素被修改
[2, 8, 51, 13, 46, 11, 22]
8:51
102:13
[2, 8, 102, 26, 46, 11, 22]
public static void parallelSetAll(int[] array, IntUnaryOperator generator):併發修改
double[]、int[]、long[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
System.out.println(Arrays.toString(a));
Arrays.parallelSetAll(a, new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
//operand當前數組的索引
System.out.println(operand);
return operand*2;
}
});
System.out.println(Arrays.toString(a));
}
}
結果:因爲是併發執行,所以索引輸出的順序是不固定的
[2, 8, 51, 13, 46, 11, 22]
4
3
5
0
6
1
2
[0, 2, 4, 6, 8, 10, 12]
public static void setAll(int[] array, IntUnaryOperator generator):單線程修改修改
double[]、int[]、long[]、T[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
System.out.println(Arrays.toString(a));
Arrays.setAll(a, new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
//operand當前數組的索引
System.out.println(operand);
return operand*2;
}
});
System.out.println(Arrays.toString(a));
}
}
結果:由於是單線程,所以operand是順序輸出的
[2, 8, 51, 13, 46, 11, 22]
0
1
2
3
4
5
6
[0, 2, 4, 6, 8, 10, 12]
public static void sort(int[] a):升序排序
char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
結果:
[2, 8, 11, 13, 22, 46, 51]
public static IntStream stream(int[] array)
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
IntStream stream = Arrays.stream(a);
System.out.println( Arrays.toString(stream.toArray()));
System.out.println(Arrays.toString(a));
}
}
結果:
[2, 8, 51, 13, 46, 11, 22]
[2, 8, 51, 13, 46, 11, 22]
public static String toString(boolean[] a):格式化輸出
char[]、double[]、float[]、int[]、long[]、Object[]、short[]均有此方法
public class Test {
public static void main(String[] args) {
int[] a = {2,8,51,13,46,11,22};
System.out.println(Arrays.toString(a));
}
}
結果:
[2, 8, 51, 13, 46, 11, 22]