1、toString
将数组拼接成字符串
public class arrays {
public static void main(String[] args) {
int[] arr = {
11,33,22,55,44};
System.out.println(Arrays.toString(arr));
}
}
2、binarySearch
用二分查找法查找元素
返回的是索引,如果不存在返回 负插入点减1
public class arrays {
public static void main(String[] args) {
int[] arr = {
11,22,33,44,55};
int i = Arrays.binarySearch(arr, 22);
System.out.println(i);
}
}
3、copyof
拷贝数组
- 新数组长度小于老数组,会部分拷贝
- 新数组长度大于老数组,会全部拷贝,再补上默认值
- 新数组长度等于老数组,会完全拷贝
public class arrays {
public static void main(String[] args) {
int[] arr = {
11,22,33,44,55};
int[] ints = Arrays.copyOf(arr, 2);
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
}
}
4、copyOfRange
指定拷贝数组
public class arrays {
public static void main(String[] args) {
int[] arr = {
11,22,33,44,55};
int[] ints = Arrays.copyOfRange(arr,0,4);
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
}
}
5、fill
填充数组
将所有值填充为100
public class arrays {
public static void main(String[] args) {
int[] arr = new int[10];
Arrays.fill(arr,100);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
6、sort
排序数组
默认情况下用升序,算法用的是快速排序
public class arrays {
public static void main(String[] args) {
int[] arr = {
11,33,54,22};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}