目录 Java常用类库Arrays 一、常用方法 1.1 toString 1.2 Sort 1.2.1 sort(T[] a, int fromIndex, int toIndex) 1.2.2 Sort(T[] a) 1.2.3 其它 1.3 copyOf 1.4 mismatch 1.5 binarySearch 1.5.1 binarySearch(T[] a, int fromIndex, int toInde
目录
- Java常用类库Arrays
- 一、常用方法
- 1.1 toString
- 1.2 Sort
- 1.2.1 sort(T[] a, int fromIndex, int toIndex)
- 1.2.2 Sort(T[] a)
- 1.2.3 其它
- 1.3 copyOf
- 1.4 mismatch
- 1.5 binarySearch
- 1.5.1 binarySearch(T[] a, int fromIndex, int toIndex, T key)
- 1.5.2 binarySearch(T[] a, T key)
- 1.5.3 其它
- 1.6 equals
- 1.6.1 equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
- 1.6.2 equals(T[] a, T[] a2)
- 1.6.3 其它
- 1.7 fills
- 1.7.1 fill(T[] a, int fromIndex, int toIndex, T val)
- 1.7.2 fill(T[] a, T val)
- 二、其他方法
- 总结
Java常用类库Arrays
类Arrays包含用于操作数组的各种方法(例如排序和搜索)
- 如果指定的数组引用为null,则此类中的方法都抛出NullPointerException ,除非另有说明
一、常用方法
1.1 toString
返回指定数组内容的字符串形式
举例
int[] a1 = {1,2,3,4,5}; System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
源码
public static String toString(int[] a) { if (a == null) return "null"; int iMax = a.length - 1; if (iMax == -1) return "[]"; StringBuilder b = new StringBuilder(); b.append('['); for (int i = 0; ; i++) { b.append(a[i]); if (i == iMax) return b.append(']').toString(); b.append(", "); } }
其它
1.2 Sort
排序(默认升序)
1.2.1 sort(T[] a, int fromIndex, int toIndex)
指定区间进行排序
举例
int[] a1 = {9,1,3,7,2,5}; System.out.println(Arrays.toString(a1));//[9, 1, 3, 7, 2, 5] Arrays.sort(a1,0,3);//[0,3),对9,1,3进行排序 System.out.println(Arrays.toString(a1));//[0[1, 3, 9, 7, 2, 5]
源码
public static void sort(int[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); }
1.2.2 Sort(T[] a)
对整个数组进行排序
举例
int[] a1 = {0,7,8,2,4,1}; System.out.println(Arrays.toString(a1));//[0, 7, 8, 2, 4, 1] Arrays.sort(a1); System.out.println(Arrays.toString(a1));//[0, 1, 2, 4, 7, 8]
源码
public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); }
1.2.3 其它
1.3 copyOf
复制(常用于数组扩容)
举例
int[] a = {1,2,3}; System.out.println(a.length);//output:3 a = Arrays.copyOf(a,15); System.out.println(a.length);//output:15
源码
public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
其它
1.4 mismatch
举例
int[] a1 = {0,1,2,3,4,5}; int[] a2 = {0,1,2,3,4,5};//与a1相同 int[] a3 = {0,1,2,3,0,5};//从索引4开始与a1不同 System.out.println(Arrays.mismatch(a1,a2));//output:-1 System.out.println(Arrays.mismatch(a1,a3));//output:4
源码
public static int mismatch(int[] a, int[] b) { int length = Math.min(a.length, b.length); // Check null array refs if (a == b) return -1; int i = ArraysSupport.mismatch(a, b, length); return (i < 0 && a.length != b.length) ? length : i; }
其它
1.5 binarySearch
二分查找,搜索,返回下标
1.5.1 binarySearch(T[] a, int fromIndex, int toIndex, T key)
限定了搜索的范围[fromIndex, toIndex)
举例
int[] a = {1,2,3,4,5}; int x1 = Arrays.binarySearch(a,2,3,4);//在a数组下标[2,3)中查找值为4的下标 System.out.println(x1);//output:<0的随机数
源码
public static int binarySearch(int[] a, int fromIndex, int toIndex,int key) { rangeCheck(a.length, fromIndex, toIndex); return binarySearch0(a, fromIndex, toIndex, key); } private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }
1.5.2 binarySearch(T[] a, T key)
与上述相同,只是没有限定范围,fromIndex=0, toIndex=length
举例
int[] a = {1,2,3,4,5}; int x1 = Arrays.binarySearch(a,3);//在a数组中查找值为3的下标 int x2 = Arrays.binarySearch(a,-6);//在a数组中查找值为6的下标 System.out.println(x1);//output:2 System.out.println(x2);//output:<0的随机数
源码
public static int binarySearch(int[] a, int key) { return binarySearch0(a, 0, a.length, key); } private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }
1.5.3 其它
1.6 equals
1.6.1 equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
如果两个指定数组在指定范围内相等 ,则返回true
举例
int[] a1 = {1,2,3,4,5}; int[] a2 = {1,2,0,0,4,5}; System.out.println(Arrays.equals(a1,0,2,a2,0,2));//true System.out.println(Arrays.equals(a1,3,5,a2,4,6));//true
源码
public static boolean equals(int[] a, int aFromIndex, int aToIndex,int[] b, int bFromIndex, int bToIndex) { rangeCheck(a.length, aFromIndex, aToIndex); rangeCheck(b.length, bFromIndex, bToIndex); int aLength = aToIndex - aFromIndex; int bLength = bToIndex - bFromIndex; if (aLength != bLength) return false; return ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex,aLength) < 0; }
1.6.2 equals(T[] a, T[] a2)
如果两个指定数组相等,则返回 true
举例
int[] a1 = {1,2,3,4,5}; int[] a2 = {1,2,3,4,5}; int[] a3 = {1,2,0,4,5}; System.out.println(Arrays.equals(a1,a2));//true System.out.println(Arrays.equals(a1,a3));//false
源码
public static boolean equals(int[] a, int[] a2) { if (a==a2) return true; if (a==null || a2==null) return false; int length = a.length; if (a2.length != length) return false; return ArraysSupport.mismatch(a, a2, length) < 0; }
1.6.3 其它
1.7 fills
1.7.1 fill(T[] a, int fromIndex, int toIndex, T val)
将指定的T值分配给指定的T类型数组的指定范围的每个元素
举例
int[] a1 = new int[10]; Arrays.fill(a1,1,4,8); char[] a2 = new char[10]; Arrays.fill(a2,0,3,'s'); System.out.println(Arrays.toString(a1));//[0, 8, 8, 8, 0, 0, 0, 0, 0, 0] System.out.println(Arrays.toString(a2));//[s, s, s, , , , , , , ]
源码
public static void fill(char[] a, int fromIndex, int toIndex, char val) { rangeCheck(a.length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++) a[i] = val; }
1.7.2 fill(T[] a, T val)
将指定的T值分配给指定的T类型数组的每个元素
举例
int[] a1 = new int[10]; Arrays.fill(a1,8); char[] a2 = new char[10]; Arrays.fill(a2,'s'); System.out.println(Arrays.toString(a1));//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8] System.out.println(Arrays.toString(a2));//[s, s, s, s, s, s, s, s, s, s]
源码
public static void fill(int[] a, int val) { for (int i = 0, len = a.length; i < len; i++) a[i] = val; }
二、其他方法
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注自由互联的更多内容!