Java中数组操纵 java.util.Arrays 类经常运用要领的运用
2019-11-18杂谈搜奇网37°c
A+ A-任何一门编程言语,数组都是最重要和经常运用的数据结构之一,但差别的言语对数组的组织与处置惩罚是不尽雷同的。
Java中供应了java.util.Arrays 类能轻易地操纵数组,而且它供应的一切要领都是静态的。下面引见一下Arrays类最经常运用的几个要领。
1. 数组排序
Arrays东西类供应了一个sort要领,只需要一行代码即可完成排序功能。
2. 数组转换为字符串
Arrays供应了一个toString要领,能够直接把一个数组转换为字符串,如许能够轻易视察数组里的元素。
//泉源:民众号【时间与字节】 //数组排序与转换为字符串 package BaseCode; import java.util.Arrays; public class j4_1028_11 { public static void main(String[] args) { int[] ff= {11,3,25,71,9}; System.out.print("数组ff未排序: "); for(int n:ff) System.out.print(n+" "); Arrays.sort(ff); // 对数组举行排序 System.out.printf("\n数组ff排序后: "); for(int n:ff) System.out.print(n+" "); //将数组转换为字符串 System.out.printf("\n数组ff转为字符串: "+Arrays.toString(ff)); } }
运转效果
数组ff未排序:11 3 25 71 9 数组ff排序后:3 9 11 25 71 数组ff转为字符串:[3, 9, 11, 25, 71]
3. 数组元素的添补与替代
Arrays供应了fill要领对数组(或数组指定位置)添补或替代为指定的值。
4. 推断数组是不是雷同
Arrays.equals能够比较两个数组中的元素是不是一样。
//泉源:【时间与字节】 //fill要领和equals要领 package BaseCode; import java.util.Arrays; public class j4_1028_12 { public static void main(String[] args) { int[] ff= new int[5]; Arrays.fill(ff, 5); System.out.print("数组悉数元素添补为5: "); for(int n:ff) System.out.print(n+" "); //将数组从第1个元素至第3个元素添补为7 //含第1个元素,不含第3个元素 Arrays.fill(ff,1,3,7); System.out.print("\n数组指定位置添补为7: "); for(int n:ff) System.out.print(n+" "); int[] nn= new int[5]; Arrays.fill(nn, 5); System.out.printf("\nff与nn雷同:"+Arrays.equals(ff, nn)); } }
运转效果
数组悉数元素添补为5:5 5 5 5 5 数组指定位置添补为7:5 7 7 5 5 ff与nn雷同:false
5. 复制数组
Arrays类的copyOf()要领和copyRange()要领能够完成对数组的复制。
copyOf(arr, int newlength)
参数newlength为新数组的长度,即从数组arr的第0个位置最先,直到newlength完毕,假如newlength大于arr的长度,背面按默认值添补。
copyOfRange(arr, int formIndex, int toIndex)
参数formIndex为从数组arr中取元素的最先位置,toIndex为完毕位置,但不包括该位置的元素,如toIndex超越arr的长度,背面按默认值添补。
//泉源:民众号【时间与字节】 //数组的复制,copyOf与copyOfRange的运用 package BaseCode; import java.util.Arrays; public class j4_1028_10 { public static void main(String[] args) { int[] ff= {1,3,5,7,9}; //Arrays.copyOf复制数组至指定长度,从0最先 int[] newff1=Arrays.copyOf(ff, 3); int[] newff2=Arrays.copyOf(ff, 6); System.out.print("copyOf的运用:\n数组newff1: "); for(int n:newff1) System.out.print(n+" "); System.out.printf("\n数组newff2: "); for(int n:newff2) System.out.print(n+" "); //Arrays.copyOfRange第二个参数为最先位置 //第三个参数为完毕位置 int[] newff3=Arrays.copyOfRange(ff, 1, 4); int[] newff4=Arrays.copyOfRange(ff, 1, 7); System.out.printf("\ncopyOfRange的运用:\n数组newff3: "); for(int n:newff3) System.out.print(n+" "); System.out.printf("\n数组newff4: "); for(int n:newff4) System.out.print(n+" "); } }
运转效果
copyOf的运用: 数组newff1:1 3 5 数组newff2:1 3 5 7 9 0 copyOfRange的运用: 数组newff3:3 5 7 数组newff4:3 5 7 9 0 0
6. 元素查询
Arrays类的binarySearch 要领能够查询元素涌现的位置,返回元素的索引。然则注重,运用binarySearch举行查找之前,必需运用sort举行排序。而且假如数组中有多个雷同的元素,查找效果是不确定的。
binarySearch(arr, object key)
假如key在数组中,则返回搜刮值的索引;不然返回-1或许负的插进去点值。
所谓插进去点值就是第一个比key大的元素在数组中的索引,而且这个索引是从1最先的。
binarySearch(arr, int fromIndex, int endIndex, object key);
fromIndex:指定局限的最先处索引(包括)
toIndex:指定局限的完毕处索引(不包括)
其搜刮效果可分为以下四种状况:
-
该搜刮键不在局限内,且大于局限(数组)内元素,返回 –(toIndex + 1);
-
该搜刮键不在局限内,且小于局限(数组)内元素,返回–(fromIndex + 1);
-
该搜刮键在局限内,但不是数组元素,由1最先计数,返回负的插进去点索引值;
-
该搜刮键在局限内,且是数组元素,由0最先计数,返回搜刮值的索引值;
可参看下面的示例代码及解释
//泉源:民众号【时间与字节】 //查找数组元素:binarySearch 要领的运用 package BaseCode; import java.util.Arrays; public class j4_1028_13 { public static void main(String[] args) { int[] fn= {1,3,5,7,9}; Arrays.sort(fn);//查找前先排序 int cx1=Arrays.binarySearch(fn,5);//返回2 ,找到了关键字,索引从0最先 //未找到6,返回的是负的插进去点值, //6在数组中的插进去点是元素7的索引, //元素7的索引从1最先算就是4,一切返回-4 int cx2=Arrays.binarySearch(fn,6);//未找到,返回-4,插进去点7的索引是4 int cx3=Arrays.binarySearch(fn,4);//未找到,返回-3,插进去点5的索引是3 System.out.println("不指定查找局限示例:"); System.out.println("数组fn的内容:"+Arrays.toString(fn)); System.out.printf("[5]找到:cx1=%d%n", cx1); System.out.printf("[6][4]未找到:cx2=%d, cx3=%d%n", cx2,cx3); //在数组的指定位置查找元素,参数局限(1,3)包括的数组元素为[3,5] //该搜刮键不在局限内,且大于局限(数组)内元素,返回 –(toIndex + 1)。 int cx4=Arrays.binarySearch(fn,1,3,10); //该搜刮键不在局限内,且小于局限(数组)内元素,返回–(fromIndex + 1); int cx5=Arrays.binarySearch(fn,1,3,-3); //该搜刮键在局限内,但不是数组元素,由1最先计数,返回负的插进去点索引值 int cx6=Arrays.binarySearch(fn,1,3,4); //该搜刮键在局限内,且是数组元素,由0最先计数,返回搜刮值的索引值 int cx7=Arrays.binarySearch(fn,1,3,5); System.out.println("-------------------------"); System.out.println("用参数指定查找局限示例:"); System.out.println("第一种状况:cx4= "+cx4); System.out.println("第二种状况:cx5= "+cx5); System.out.println("第三种状况:cx6= "+cx6); System.out.println("第四种状况:cx7= "+cx7); } }
运转效果
不指定查找局限示例: 数组fn的内容:[1, 3, 5, 7, 9] [5]找到:cx1=2 [6][4]未找到:cx2=-4, cx3=-3 ------------------------- 用参数指定查找局限示例: 第一种状况:cx4= -4 第二种状况:cx5= -2 第三种状况:cx6= -3 第四种状况:cx7= 2