3.7 操作数组的工具类-Arrays

一、基本的Arrays类

(1)查找数组元素的索引值:

static int binarySearch(type[] a, type key): 使用二分搜索法来搜索key元素在数组中的索引;若a数组不包括key,返回负数。(该方法必须已按升序排列后调用)。

static int binarySearch(type[] a, int fromIndex, int toIndex, type key): 使用二分搜索法来搜索key元素在数组中从fromIndex到toIndex的索引;若a数组不包括key,返回负数。(该方法必须已按升序排列后调用)。

 1 import java.util.Arrays;
 2 public class 索引查找 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int[] a=new int[]{1,2,5,7,12,23,45,50};
 7         for(int ele:a)
 8         {
 9             System.out.print("  "+ele);
10         }
11         System.out.println();
12 
13         int index = Arrays.binarySearch(a,12);
14         System.out.println(index);
15         int index1 = Arrays.binarySearch(a,2,4,12);//左闭右开[2,4)
16         System.out.println(index1);
17 
18     }
19 }
20 ---------- 运行java(捕获窗口) ----------
21   1  2  5  7  12  23  45  50
22 4
23 -5
24 
25 输出完成 (耗时 0 秒) - 正常终止
View Code

 (2)复制数组

 static boolean[] copyOf(type[] original, int newLength) :这个方法会把original数组复制成一个新数组,其中length是新数组的长度,如果length小于original数组的长度,则新数组就是原数组的前面length个元素;如果length大于original数组的长度,则新数组的前面元素是原数组的所有元素,后面补充0(整数类型)、false(布尔类型)、null(引用类型)。

static byte[] copyOfRange(type[] original, int from, int to): 将数组的指定范围复制到一个新数组。(左闭右开区间) 

 1 import java.util.Arrays;
 2 class 数组复制 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int [] a=new int[]{1,8,7,15,12};
 7         int [] b=Arrays.copyOf(a,2);
 8 
 9         //复制数组的长度小于length
10         for(int i:b)
11         {
12             System.out.print(" "+i);
13         }
14         System.out.println();
15 
16         //复制的数组长度大于length
17         int[] c=Arrays.copyOf(a,8);
18         for(int ele:c)
19         {
20             System.out.print("  "+ele);
21         }
22         System.out.println();
23 
24         //复制一定范围的数组
25         int[] d=Arrays.copyOfRange(a,1,3);
26         for(int ele:d)
27         {
28             System.out.print("  "+ele);
29         }
30     }
31 }
32 ---------- 运行java(捕获窗口) ----------
33  1 8
34   1  8  7  15  12  0  0  0
35   8  7
36 输出完成 (耗时 0 秒) - 正常终止
View Code

另外system类里还包含一个数组复制的类——static void arraycopy(Object src,int srcPos,Object dest,int destpos,int length):该方法将src数组的元素赋给dest数组元素,其中srcPos是指从源数组开始赋值的起始索引值,length指定将src数组的多少个元素赋给dest数组。desPos是复制到dest数组开始的索引值。

 1 import java.util.Arrays;
 2 class arraycopytest 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int a[]=new int[]{1,2,4,3,7,8,9};
 7         int b[]=new int[]{0,0,0,0,0,0,0};
 8         for(int ele:b)
 9         {
10             System.out.print("  "+ele);
11         }
12         System.out.println();
13 
14         System.arraycopy(a,1,b,2,3);
15         for(int ele:b)
16         {
17             System.out.print("  "+ele);
18         }
19         System.out.println();
20     }
21 }
22 ---------- 运行java(捕获窗口) ----------
23   0  0  0  0  0  0  0
24   0  0  2  4  3  0  0
25 
26 输出完成 (耗时 0 秒) - 正常终止
View Code

(3)判断两个数组是否相等

static boolean equals(type[] a, type[] a2) 如果两个数组长度相等和元素一一相等,则返回 true

 1 import java.util.Arrays;
 2 class 数组相等 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int[] a=new int[]{1,5,9};
 7         int[] b=new int[]{1,5,9};
 8         int[] c=new int[]{3,6,9};
 9 
10         System.out.println("a==b?:"+Arrays.equals(a,b));
11         System.out.println("a==b?:"+Arrays.equals(a,c));
12     }
13 }
14 ---------- 运行java(捕获窗口) ----------
15 a==b?:true
16 a==b?:false
17 
18 输出完成 (耗时 0 秒) - 正常终止
View Code

(4)数组填充

static void fill(type[] a, type val) 将a数组所有元素都赋为val。

static void fill(type[] a, int fromIndex, int toIndex, type val) 将a数组从formIndex 到tiondex索引之间的元素都赋为val。 

 1 import java.util.Arrays;
 2 class 数组填充 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int a[]=new int[]{8,7,1,5,7};
 7         //原始数组
 8         for(int ele:a)
 9         {
10             System.out.print("  "+ele);
11         }
12         System.out.println();
13 
14         Arrays.fill(a,10);
15         for(int ele:a)
16         {
17             System.out.print("  "+ele);
18         }
19         System.out.println();
20 
21         Arrays.fill(a,1,3,0);
22         for(int ele:a)
23         {
24             System.out.print("  "+ele);
25         }
26     }
27 }
28 ---------- 运行java(捕获窗口) ----------
29   8  7  1  5  7
30   10  10  10  10  10
31   10  0  0  10  10
32 输出完成 (耗时 0 秒) - 正常终止
View Code

(5)数组排序

static void sort(type[] a) //sort(int[] arr)对指定的数组按数字升序进行排序。

static void sort(type[] a, int fromIndex, int toIndex) 对指定数组的从formIndex 到tiondex索引之间的元素按数字升序进行排序。(左闭右开)

 1 import java.util.Arrays;
 2 class 数组排序 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int[] a=new int[]{9,5,7,6,5,4};
 7         //原数组
 8         for(int ele:a)
 9         {
10             System.out.print("  "+ele);
11         }
12         System.out.println();
13 
14         //排序后的数组
15         Arrays.sort(a);
16         for(int ele:a)
17         {
18             System.out.print("  "+ele);
19         }
20         System.out.println();
21     }
22 }
23 ---------- 运行java(捕获窗口) ----------
24   9  5  7  6  5  4
25   4  5  5  6  7  9
26 
27 输出完成 (耗时 0 秒) - 正常终止
View Code

(6)转换为字符串

static String toString(type[] a) 返回指定数组内容的字符串表示形式。多个数组元素之间用英文逗号或空格隔开。

 1 import java.util.Arrays;
 2 class 转换字符串 
 3 {
 4     public static void main(String[] args) 
 5     {
 6         char[] arr=new char[]{'q','u','i','t'};
 7         for(char ele:arr)
 8         {
 9             System.out.print(" "+ele);
10         }
11         System.out.println();
12 
13         //转换为字符串
14         String str=Arrays.toString(arr);
15         System.out.println(str);
16     }
17 }
18 ---------- 运行java(捕获窗口) ----------
19  q u i t
20 [q, u, i, t]
21 
22 输出完成 (耗时 0 秒) - 正常终止
View Code
posted @ 2020-02-17 20:48  小新和风间  阅读(161)  评论(0编辑  收藏  举报