- 给数组赋值(填充):通过 Arrays.fill 方法
public class Array14 {
public static void main(String[] args) {
int[] a = {1,2,3,323,43432,33333,32};
Arrays.fill(a,1,2,55);
System.out.println(Arrays.toString(a));
Arrays.fill(a,55);
System.out.println(Arrays.toString(a));
}
}
- 打印数组元素:通过 Arrays.toString() 方法
public class ArrayDemo05 {
public static void main(String[] args) {
int[] a = {1,2,3,323,43432,33333,32};
int[] b = {1,3,4,55,232,43421,323155,22};
System.out.println(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b ));
}
}
- 对数组排序:通过 Arrays.sort 方法,按升序
public class ArrayDemo05 {
public static void main(String[] args) {
int[] a = {1,2,3,323,43432,33333,32};
int[] b = {1,3,4,55,232,43421,323155,22};
Arrays.sort(a);
Arrays.sort(b);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}
}
- 比较数组:通过 Arrays.equals 方法比较数组中元素值是否相等
public class ArrayDemo05 {
public static void main(String[] args) {
int[] a = {1,2,3,323,43432,33333,32};
int[] b = {1,3,4,55,232,43421,323155,22};
int[] c = {1,2,3,323,43432,33333,32};
Arrays.equals(a,b);
System.out.println(Arrays.equals(a,b));
Arrays.equals(a,c);
System.out.println(Arrays.equals(a,c));
}
}
- 查找数组元素:通过 Arrays.binarySearch 方法能对排序好的数组进行二分查找法操作
public class ArrayDemo05 {
public static void main(String[] args) {
int[] a = {1,2,3,323,43432,33333,32};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
Arrays.binarySearch(a,43432);
System.out.println(Arrays.binarySearch(a,43432));
}
}
- 复制数组元素:通过 Arrays.copyOfRange 方法进行数组复制操作
public class Array14 {
public static void main(String[] args) {
int[] a = new int[]{11,43,55,64,42,43};
int[] b = Arrays.copyOfRange(a,2,5);
for (int i : b) {
System.out.print(i+" ");
}
}
}