作为一个稀有的Java妹子,所写的所有博客都只是当作自己的笔记,留下证据自己之前是有用心学习的~哈哈哈哈(如果有不对的地方,也请大家指出,不要悄悄咪咪的不告诉我)
Arrays
数组的工具类,主要是提供了一些对数组操作的一些方法。
方法举例
1.sort排序
int[] a = {14,2,37,12};
//支持的排序类型int long short char byte float double
Arrays.sort(a);
System.out.println(JSONObject.toJSONString(a));
//[2,12,14,37]
int[] b = {14,2,37,12};
//只对0-2下标元素排序
Arrays.sort(b,0,2);
System.out.println(JSONObject.toJSONString(b));
//[2,14,37,12]
2.parallelSort并行排序
//在数据量很大的情况下,效率高
int[] a = {14,2,37,12};
Arrays.parallelSort(a);
System.out.println(JSONObject.toJSONString(a));
//[2,12,14,37]
3.binarySearch查找元素下标
这个方法在使用时,输入头尾元素的话,下标不对,具体原因不清楚
int[] a = {14,2,37,12};
int i = Arrays.binarySearch(a, 2);
System.out.println(i);
//1
4.equals判断两个数组的所有元素是否都相等
int[] a = {14,2,37,12};
int[] b = new int[]{14,2,37,12};
boolean equals = Arrays.equals(a, b);
System.out.println(equals);
//true
int[] c = new int[]{1,2,3,4};
equals = Arrays.equals(a, c);
System.out.println(equals);
//false
5.fill使用某个元素填充数组的所有元素
int[] a = {14,2,37,12};
Arrays.fill(a,1);
System.out.println(JSONObject.toJSONString(a));
//[1,1,1,1]
int[] b = {14,2,37,12};
//填充下标0-2的元素(左闭右开)为1
Arrays.fill(b,0,2,1);
System.out.println(JSONObject.toJSONString(b));
//[1,1,37,12]
6.copyOf对数组容量扩容
这个方法是HashMap,Arraylist扩容的底层方法
int[] a = {14,2,37,12};
a = Arrays.copyOf(a,9);
System.out.println(a.length);
//9
7.asList将数组转为集合
String[] str = new String[]{"111","222","333"};
List<String> list = Arrays.asList(str);
System.out.println(JSONObject.toJSONString(list));
//["111","222","333"]
8.toString转为字符串
int[] a = {14,2,37,12};
String s = Arrays.toString(a);
System.out.println(s);
//[14, 2, 37, 12]
本文详细介绍了Java中Arrays工具类的使用方法,包括排序、并行排序、二分查找、数组比较、数组填充、数组复制及数组转换为集合等功能,为Java开发者提供了实用的代码示例。
614

被折叠的 条评论
为什么被折叠?



