下面代码主要说明了Arrays数组的几个常用方法(红色字体)
import java.util.Scanner;
import java.util.Arrays;
public class T{
public static void main(String[] args){
int[] array1 = {"2","65","7","42","69"};
int[] array2 = new int[10];
String[] A = {"a","h","g","G","H","4"};
String[] B = new String[3];
System.out.print("array1排序前:");
for(int i=0;i<array1.length,i++){
System.out.print(array1[i]+" ");
}
Arrays.sort(array1);//Array.sort(要排序的数组)
System.out.print("array1排序后:");
for(int i=0;i<array1.length,i++){
System.out.print(array1[i]+" ");
}
System.out.print("请输入搜索值:");
int key = Scanner.nextInt();
int find = -1;
if(find = Arrays.binarySearch(array,key)>-1){ //Arrays.binarySearch(数组名,要寻找的值)
System.out.println("找到值位于索引" + find + "位置");
}else{
System.out.print("找不到指定值");
}
Arrays.fill(array2,5);//Array.fill(数组名,给数组的赋值的元素)
for(int j=0;j<array2.length;j++){
System.out.println("array2:"+ array2[j] + " ");
}
System.out.println("array1 = array2:" + Arrays.equals(array1,array2));
System.arraycopy(A,0,B,1,B.length-1); //五个参数分别为(源数组,开始下标,目标数组,目标数组开始下标,复制元素长度)
for(inti=0;i<B.length;i++){
System.out.println("数组B:"+ B[i] +" ");
}
}
执行结果:排序前:2,65,7,42,69
排序后:2,7,42,65,69
请输入搜索值:7
找到值位于索引 2位置
array2:5 5 5 5 5 5 5 5 5 5
array1 = array2: false
数组B:null a h
Arrays数组的常用方法