Arrays类
Arrays类:此类包含用来操作数组的各种方法(针对数组操作的工具类).
方法:
1.public static String toString(int[ ] a):将任意数组以字符串形式显示出来.
2.public static void sort(int[ ] a):将任意数组进行快速排序(升序).
3.public static int binarySearch(int[ ] a,int key):将任意数组(前提是经过排序)进行二分查找.
4.public static boolean equals(int[ ] a,int[ ] a2) :如果两个指定类型数组彼此相等,则返回true.
相关代码:
import java.util.Arrays;
public class Arraydemo {
public static void main(String[] args) {
int[] arr={40,10,20,90,70,80};
//将数组转化为字符串
System.out.println("toString:"+Arrays.toString(arr));
//将数组进行排序
Arrays.sort(arr);
System.out.println("sort:"+Arrays.toString(arr));
//将排序后的数组进行二分查找
System.out.println("binarySearch:"+Arrays.binarySearch(arr, 20));//找到
System.out.println("binarySearch:"+Arrays.binarySearch(arr, 100));//未找到
int[] arr2={20,10,50};
System.out.println("equals:"+Arrays.equals(arr, arr2));
}
}
结果为:toString:[40, 10, 20, 90, 70, 80]sort:[10, 20, 40, 70, 80, 90]
binarySearch:1
binarySearch:-7
equals:false