Java中数组操作 java.util.Arrays 类常用方法的使用

时间:2019-11-04
本文章向大家介绍Java中数组操作 java.util.Arrays 类常用方法的使用,主要包括Java中数组操作 java.util.Arrays 类常用方法的使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

任何一门编程语言,数组都是最重要和常用的数据结构之一,但不同的语言对数组的构造与处理是不尽相同的。

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:指定范围的结束处索引(不包含

其搜索结果可分为以下四种情况:

  1. 该搜索键不在范围内,且大于范围(数组)内元素,返回 –(toIndex + 1);

  2. 该搜索键不在范围内,且小于范围(数组)内元素,返回–(fromIndex + 1);

  3. 该搜索键在范围内,但不是数组元素,由1开始计数,返回负的插入点索引值;

  4. 该搜索键在范围内,且是数组元素,由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

原文地址:https://www.cnblogs.com/nnzhang/p/11791497.html