栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中操作数组的Arrays类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中操作数组的Arrays类

Arrays类

引包:位于java.util 包下。

Arrays类包含了各种操作数组的静态方法:

数组排序:sort(升序排序)重载了各种数组升序排序方法,举例几种:

sort(char[] a)sort(double[] a)sort(int[] a)

示例:

public class ArraysTest {
    public static void main(String[] args) {
        int[] arrInt = {9,8,7,6,5,4,3,2,1};
        Arrays.sort(arrInt);
         for(int x: arrInt){
            System.out.print(x + " ");	//打印结果1 2 3 4 5 6 7 8 9 
        }
    }
}

数组查询:binarySearch(二分查找)重载了各种数组查询方法,举例几种:

binarySearch(double[] a, double key)binarySearch(int[] a, int key)binarySearch(char[] a, char key)

​ 第一个参数都是传进来的数组,第二个参数是要查找的值。如果存在查询的值,返回该数在数组中的下标,否则返回(-(insertion point)-1);示例:

public class ArraysTest {
    public static void main(String[] args) {
        int[] arrInt = {9,8,7,6,5,4,3,2,1};
        Arrays.sort(arrInt);		//排序
        int t = Arrays.binarySearch(arrInt, 20); //必须要先排序才能使用此方法!!
        System.out.println(t);		//结果为-10
    }
}

数组打印:toString 重载了各种数组打印方法,举例几种:

toString(boolean[] a)toString(char[] a)toString(double[] a)toString(int[] a)

返回结果是字符串,如果数组为null,返回null。示例:

public class ArraysTest {
    public static void main(String[] args) {
        int[] arrInt = {9,8,7,6,5,4,3,2,1};
        String s = Arrays.toString(arrInt);
        System.out.println(s);	//结果[9, 8, 7, 6, 5, 4, 3, 2, 1]
    }
}

数组复制:copyOf 和copyOfRange重载了各种数组复制方法,举例两种常见方法:

copyOf(int[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。copyOfRange(int[] original, int from, int to) 将指定数组的指定范围复制到新数组中。

示例:

public class ArraysTest {
    public static void main(String[] args) {
        int[] arrInt = {8,7,6,5,4,3,2,1};
        int[] arr = Arrays.copyOf(arrInt, 9);	//复制到具有9个位置的新数组中
        int[] arr2 = Arrays.copyOfRange(arrInt, 1,5);	//复制下标1-5元素到新数组中
        System.out.println(Arrays.toString(arr));	//结果[8, 7, 6, 5, 4, 3, 2, 1, 0]
        System.out.println(Arrays.toString(arr2));	//结果[7, 6, 5, 4]
    }
}

数组比较:equals 重载了各种类型数组比较,如:

equals(int[] a, int[] a2) 如果两个指定的int数组彼此 相等 ,则返回 true 。equals(double[] a, double[] a2) 如果两个指定的双精度数组彼此 相等 ,则返回 true 。

需要注意:

1.比较的两个数组类型需要相同!!!

2.如果比较的数组引用都为null,返回true。

示例:

public class ArraysTest {
    public static void main(String[] args) {
        int[] arrInt = {7,6,5,4,3,2,1};
        int[] arrInt2 = {7,6,5,4,3,2,1};
        boolean flag = Arrays.equals(arrInt, arrInt2);
        System.out.println(flag);	//结果是true
    }
}

数组填充:fill 重载了各种类型数组填充方式,以int型举例两种填充方式:

fill(int[] a, int val) 将指定的val值分配给指定的int数组的每个元素。fill(int[] a, int fromIndex, int toIndex, int val) 将指定的val值分配给指定的int数组的指定范围(区间左闭右开)的每个元素。

示例:

public class ArraysTest {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Arrays.fill(arr, 2, 5, 8);	//填充左闭右开区间下标[2,5)的元素
        String s = Arrays.toString(arr);
        System.out.println(s);	//打印[0, 0, 8, 8, 8, 0]
    }
}

如有错误之处,请各位在评论区留言,欢迎大佬们指点,不胜感激!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731353.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号