Arrays类
源码位置
rt.jar/java.util包下
源码类
public class Arrays
认识
概念:表示数组操作类
- sort排序相关方法:
sort 适合一维数组升序排序,JDK1.2单线程,下标排序规则[fromIndex,toIndex),适合小量数据 parallelSort 适合一维数组升序排序,JDK1.8多线程,下标排序规则[fromIndex,toIndex),适合大量数据 - 输出相关方法:
toString 输出一维数组内容,结果是String类型,若原类型输出则用for循环 deepToString 输出多维数组内容,结果是String类型,若原类型输出则用多重for循环 - 二分法搜索方法:
binarySearch 对一维数组搜素,且需提前对数组sort,下标排序规则[fromIndex,toIndex) - 数组填充方法:
fill 对一维数组值进行填充,下标排序规则[fromIndex,toIndex) - 数组类型转换:
asList 不适合数据类型数组,只适合类数组做类型转换 - 数组比对:
equals 对一维数组值比对 deepEquals 对多维数组值比对
总结:
sort,parallelSort,toString,deepToString,binarySearch,fill方法入参都支持8大数据类型+Object数组
这里只介绍其中部分数据类型,其他类型同理
类方法
public static void sort(int[] a)
public static void sort(int[] a, int fromIndex, int toIndex)
public static void parallelSort(int[] a)
public static void parallelSort(int[] a, int fromIndex, int toIndex)
public static String toString(int[] a)
public static String deepToString(Object[] a)
public static int binarySearch(int[] a, int key)
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
public static void fill(int[] a, int val)
public static void fill(int[] a, int fromIndex, int toIndex, int val)
public static <T> List<T> asList(T... a)
public static boolean equals(int[] a, int[] a2)
public static boolean deepEquals(Object[] a1, Object[] a2)
例子
int a[] = {5,2,8,1,9};
int[][] b = {{1,2,3},{4,5,6}};
String c[] = {"u","c","e","d"};
int d[] = {5,2,8,1,9};
int[][] e = {{1,2,3},{4,5,6}};
Arrays.sort(a);
Arrays.parallelSort(d);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.deepToString(b));
System.out.println(Arrays.binarySearch(a,0,3,5));
Arrays.fill(a,0,2,3);
System.out.println(Arrays.toString(a));
List<String> list = Arrays.asList(c);
for (String value : list) {
System.out.println(value);
}
System.out.println(Arrays.equals(a, d));
System.out.println(Arrays.deepEquals(b, e));
结果
[1, 2, 5, 8, 9]
[[1, 2, 3], [4, 5, 6]]
2
[3, 3, 5, 8, 9]
u
c
e
d
false
true
|