java中的数组的Arrays工具类的使用
package day04.d1.shuzu;
import java.util.Arrays;
/**
* Arrays 工具类
* @author Administrator
*
*/
public class Arrays__________Demo {
public static void main(String[] args) {
/**
* 1、Arrays.binarySearch
*/
int[] arr={30,2,5,3,7,1,6,4,12,56,45,33};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int index=Arrays.binarySearch(arr,7);
System.out.println(index);
/**
* 2、Arrays.copyOf
*/
int[] copyArr;
copyArr=Arrays.copyOf(arr, 5);//arr 被拷贝的数组 5 复制前五个元素
System.out.println("copyArr: "+Arrays.toString(copyArr));
System.out.println(copyArr.length);
int[] copyArr2;
copyArr2=Arrays.copyOfRange(arr, 2, 5);//包含数组下标为2的元素,不包含数组下标为5的元素
System.out.println(Arrays.toString(copyArr2));
/**
* 3、Arrays.fill
*/
int[] fillArr=new int[5];
Arrays.fill(fillArr, 5); //填充元素
System.out.println(Arrays.toString(fillArr));
/*
* hashcode是怎么进行计算的
*
*if (a == null)
return 0;
int result = 1;
for (int element : a)
result = 31 * result + element;
*
*/
/**
* 4、Arrays.hashCode
*/
int hc=Arrays.hashCode(arr);
System.out.println("hashCode "+hc);
// int[] brr=null;
// System.out.println(brr.hashCode()); // 0
int[] brr1={1};
int res=brr1.hashCode();
System.out.println("brr1 "+res); // 32
int result = 1;
for (int element : brr1)
result = 31 * result + element;
System.out.println("brr1-------"+result);
System.out.println("----------------------------");
int[] brr2={1,1};
System.out.println("brr2 "+brr1.hashCode()); // 31*32+1=993
System.out.println("计算是有问题,");
System.out.println("****************************************************************");
/**
* 5、Arrays.sort
*/
Arrays.sort(arr);
System.out.println("Arrays.sort(arr) "+arr);
/**
* 6、Arrays.toString
*/
String str=Arrays.toString(arr);
System.out.println("Arrays.toString(arr) "+str);
}
}