zoukankan      html  css  js  c++  java
  • Arrays工具类常用方法演示

    java.util.Arrays是JDK中操作数组的工具类,包含了用来操作数组(比如排序和搜索)的各种方法。

    下面我们以int类型数组为例,学习下常用的方法,其他类型数组都差不多。

    1.equals(int[] a, int[] b)方法:判断两个数组是否相等

        int[] array1 = new int[]{1, 2, 3, 4};
        int[] array2 = new int[]{1, 2, 3, 4};
        int[] array3 = new int[]{1, 3, 2, 4};
        boolean b1 = Arrays.equals(array1, array2);
        boolean b2 = Arrays.equals(array1, array3);
        System.out.println(b1);// 返回true
        System.out.println(b2);// 返回false
    

    2.toString(int[] a)方法:返回一个指定数组的字符串表现形式

        int[] array1 = new int[]{1, 2, 3, 4};
        System.out.println(Arrays.toString(array1));
    	// 输出结果为[1, 2, 3, 4]
    

    3.fill(int[] a, int value)方法:给指定数组的每个元素分配指定的值

        int[] array1 = new int[5];
        Arrays.fill(array1, 1);
        System.out.println(Arrays.toString(array1));
    	// 输出结果为[1, 1, 1, 1, 1]
    

    4.sort(int[] a):按升序对指定数组进行排序

        int[] array = new int[]{99, 23, 33, 0, 65, 9, 16, 84};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    	// 输出结果为[0, 9, 16, 23, 33, 65, 84, 99]
    

    5.binarySearch(int[] a, int value):使用二分搜索算法在指定的数组中搜索指定的值,并返回该值所在索引位置;若查询不到,则返回-1

        int[] array = new int[]{1, 17, 20, 44, 45, 62, 79, 88, 93};
        int i = Arrays.binarySearch(array, 44);
        System.out.println(i);
    	// 输出结果为3
    

  • 相关阅读:
    Appium
    iOS 定位方式 iOSNsPredicateString 详解
    Appium 遇到 Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65 的解决方法
    自动化工具 appium 在真机上测试的配置 (使用个人 Apple ID)
    查看iOS App的bundleId
    阿里巴巴热招求推荐求转发
    一文读懂网络协议
    Idea生成Javadoc
    系统监控
    Hystrix使用详解
  • 原文地址:https://www.cnblogs.com/sum-41/p/11151887.html
Copyright © 2011-2022 走看看