zoukankan      html  css  js  c++  java
  • Java集合之Arrays 剖析

      Arrays工具类位于 java.util 包下,是一个比较常用的工具类,其可以针对数组进行各种操作,比如赋值、排序、搜索等等。在项目过程中我们针对数组的有关操作,如果不方便直接操作,均可通过调用此类的某些方法转换成 list 集合进行操作。下面首先介绍 Arrays 类的几种常用方法。

     

    1.   Arrays.asList

    此方法是将一个数组直接转换为集合,返回结果为 List集合。下面的代码是该方法的应用。需要注意的是,数组是Integer类型和 int 类型,生成的List 的长度是不一样的。此外,最容易踩的坑是 不能对通过数组生成的 List集合进行 add remove

     1        /**
     2          * 关于  Arrays.asList()
     3          */
     4         Integer[] a = {1,2,3,4,5,6,7};
     5         int[] b = {11,22,33,44,55};
     6 
     7         System.out.println("======关于 Arrays.asList=======");
     8         List aList = Arrays.asList(a);
     9         System.out.println(aList.size()); //运行结果为 7
    10         List bList = Arrays.asList(b);
    11         System.out.println(bList.size()); //运行结果为 1
    12         // aList.add(123); // java.lang.UnsupportedOperationException
    13         // bList.remove(1); // java.lang.UnsupportedOperationException        

    为什么不能对通过数组生成的 List集合进行 add remove

    看源码!!!首先 asList 方法返回的是一个 ArrayList类,这个类不是我们java集合中的 ArrayList,这个ArrayList 集合继承了 AbstractList,我们在调用 add 方法或者 remove 方法时都是调用的 AbstractList 中的 add 或者 remove 方法,这个方法本身就会抛出异常,因此针对通过数组生成的List类不能直接对其进行add或者remove。

      

       

     

    2.   Arrays.fill

    该方法的主要作用是对应一个给定的数组,对其进行赋值操作,其可以支持对于该数组的全部元素进行赋值,也可以支持对于给定数组的给定起始位置进行赋值,当然其符合“左闭右开”原则,即包括起始元素但不包括结束元素,与 StringsubString方法中只包括 beginIndex,不包括 endIndex一样。

     1 /**
     2   * 关于 Arrays.fill
     3   * 若是 int 类型, 未赋值元素为0; 若是 Integer 类型, 未赋值元素为 null;
     4    */
     5    Integer[] c = new Integer[10];
     6    Integer[] d = new Integer[10];
     7    Arrays.fill(c,33);
     8    Arrays.fill(d,6,8,33);
     9    System.out.println("============关于 Arrays.fill============");
    10    System.out.println(Arrays.asList(c)); // [33, 33, 33, 33, 33, 33, 33, 33, 33, 33]
    11    System.out.println(Arrays.asList(d)); // [null, null, null, null, null, null, 33, 33, null, null]

     

    3.   Arrays.equals

    该方法的主要作用是比较两个数组是否相等,不用我们手动比较两个数组的每个元素是否相等。当然,如果两个数组只是某些元素的顺序不同,其返回结果也是不相等的。

     1         /**
     2          * 关于 Arrays.equals
     3          */
     4         int[] e1 = new int[]{1,2,3};
     5         int[] e2 = null;
     6         int[] e3 = new int[]{};
     7         int[] e4 = new int[]{1,2,3};
     8         int[] e5 = new int[]{1,3,2};
     9         System.out.println("=======关于 Arrays.equals========");
    10         System.out.println(Arrays.equals(e1,e2)); //false
    11         System.out.println(Arrays.equals(e1,e3)); //false
    12         System.out.println(Arrays.equals(e1,e4)); //true
    13         System.out.println(Arrays.equals(e2,e3)); //false
    14         System.out.println(Arrays.equals(e1, e5)); //false     

      具体怎么实现的呢?看源码!!!只不过是先针对null的情况进行判断,然后根据数据的长度进行判断,最后再依次比较每个元素是否相等

       

     

    4.   Arrays.toString

    该方法比较简单,主要是将数组打印出来,对于Integer int 类型的数组均可实现,其实现源码如下,通过 StringBuilder进行拼接。

    1       /**
    2          * 关于  Arrays.toString()
    3          */
    4         Integer[] f = {1,2,3,4,5,6,7};
    5         int[] g = {1,2,3,4,5,6,7};
    6 
    7         System.out.println("========关于 Arrays.toString=========");
    8         System.out.println(Arrays.toString(f)); //运行结果为 [1, 2, 3, 4, 5, 6, 7]
    9         System.out.println(Arrays.toString(g)); //运行结果为 [1, 2, 3, 4, 5, 6, 7]

     

    5.   Arrays.sort() Arrays.binarySearch()方法

      这两个也是比较常用的方法。Arrays.sort()是对给定的数组进行排序,当然可以重写其比较方法更高排序的顺序,默认是升序,具体可参见博文 https://www.cnblogs.com/Demrystv/p/11564054.html ; Arrays.binarySearch()方法是对排序好的方法采用二分查找的方式进行查找,返回目标元素在数组中的位置。在此不再赘述。

     

     

  • 相关阅读:
    bootstrap学习(全局CSS样式)(二)
    第八周
    bootstrap学习(全局CSS样式)(一)
    变量的解构赋值 ES6
    ES6的let和const命令
    github之怎么上传本地项目
    浅谈js对象及对象属性
    解决sublime text3 文件名,小框框的办法
    web04--cookie
    web03--session
  • 原文地址:https://www.cnblogs.com/Demrystv/p/11608109.html
Copyright © 2011-2022 走看看