Java API ——Arrays类

时间:2023-03-08 16:30:20
1、Arrays类概述
  · 针对数组进行操作的工具类。
  · 提供了排序,查找等功能。
2、成员方法
  · public static String toString(int[] a):in[] a可以改为其他类型的数组,把数组转成字符串
  · public static void sort(int[] a):将各种类型的数组进行升序排序
  · public static int binarySearch(int[] a,int key):将各种类型的数组进行二分查找
public class ArraysDemo01 {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
// public static String toString(int[] a) 把数组转成字符串
System.out.println("排序前:"+ Arrays.toString(arr)); //排序前:[24, 69, 80, 57, 13]
// public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:"+Arrays.toString(arr)); //排序后:[13, 24, 57, 69, 80]
int[] arr2 = {13, 24, 57, 69, 80};
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:"+Arrays.binarySearch(arr2,57)); //binarySearch:2
System.out.println("binarySearch:"+Arrays.binarySearch(arr2,557)); //binarySearch:-6 return -(low+1)
}
}

3、Arrays类常用方法源码详细解释

public static String toString(int[] a)
public static void sort(int[] a) 底层是快速排序
public static int binarySearch(int[] a,int key)
开发原则:
只要是对象,我们就要判断该对象是否为null。
测试代码:
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr));
sort源码:
public static String toString(int[] a) {
//a -- arr -- { 24, 69, 80, 57, 13 }
if (a == null)
return "null"; //说明数组对象不存在
int iMax = a.length - 1; //iMax=4;
if (iMax == -1)
return "[]"; //说明数组存在,但是没有元素。
StringBuilder b = new StringBuilder();
b.append('['); //"["
for (int i = 0; ; i++) {
b.append(a[i]); //"[24, 69, 80, 57, 13"
if (i == iMax)
//"[24, 69, 80, 57, 13]"
return b.append(']').toString();
b.append(", "); //"[24, 69, 80, 57, "
}
}
-----------------------------------------------------
测试代码:
int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
binarySearch源码:
public static int binarySearch(int[] a, int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//key -- 577
return binarySearch0(a, 0, a.length, key);
}
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//fromIndex -- 0
//toIndex -- 5
//key -- 577 int low = fromIndex; //low=0
int high = toIndex - 1; //high=4
while (low <= high) {
int mid = (low + high) >>> 1; //mid=2,mid=3,mid=4
int midVal = a[mid]; //midVal=57,midVal=69,midVal=80
if (midVal < key)
low = mid + 1; //low=3,low=4,low=5
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}