zoukankan      html  css  js  c++  java
  • 集合与数组,集合与集合之间的转换


    1、集合与数组之间的转换

    集合中如List和Set,都和数组有着高度的相似性,既然如此,实际上集合转数组也是情理之中的事情。

    1.1 集合转数组 toArray

    看几个常用的集合类,如上图继承关系图,可以看到他们都集成了一个抽象类AbstractCollection,这个类中就实现了Collection接口的两个方法:
    • toArray ( )
    • toArray ( T[ ] a )
     
    前者的返回值是一个Object的数组,Object[],这个数组是不可以进行强制数据类型转换的,如下:
    List<Integer> pod = new ArrayList<Integer>(); 
    
    for(int i = 0; i < 5; i++){
      pod.add(i);
    }
    
    Integer[] t=(Integer[])pod.toArray();

    结果就是报错:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

    所以像如上情况,就应该使用第二种方法:
    • toArray(T[ ] a) 
    • 如果数组a的长度足够放入整个集合的数据,那么所有数据会放入到数组a当中
    • 如果数组a的长度不够,那么会把所有数据放入到一个新的数组中返回

    List<Integer> pod=new ArrayList<Integer>();
    
    for(int i=0;i<5;i++){
        pod.add(i);
    }
    
    //第一种
    Integer[] t=new Integer[pod.size()];
    pod.toArray(t);
    
    //第二种
    Integer[] t3=new Integer[1];
    Integer[] t2=pod.toArray(t3);

    即以上的 t 和 t2 都是我们期望得到的数组。

    1.2 数组转集合 asList

    数组转集合,就要利用到数组的工具类Arrays了,该类中定义了数组转为List的一个方法:
    • asList(T... a)
    • 返回一个装载了数组a中所有数据的固定长度的List

    public static void main(String[] args) {
        String[] pod = new String[]{"haha", "xixi", "heihei"};
        List<String> list = Arrays.asList(pod);
    
        for (String bean : list) {
            System.out.println(bean);
        }
    }

    如果希望转换为长度可变的List,则利用ArrayList构造方法:
    public static void main(String[] args) {
        String[] pod = new String[]{"haha", "xixi", "heihei"};
        List<String> list = Arrays.asList(pod);
        List<String> newList = new ArrayList<String>(list);
    
        for (String bean : newList) {
            System.out.println(bean);
        }
    }


    2、集合与集合之间的转换 addAll

    刚才我们提到了集合类的继承关系图,在抽象类AbstractCollection,还实现了Collection接口中的一个方法,叫addAll,具体实现类对该方法多少进行了重写:
     
    这里返回的布尔值是指,如果List集合对象由于调用addAll方法而发生更改,则返回true

    如下示例,TreeSet转换为List类型:
    public static void main(String[] args) {
        TreeSet<Integer> pod = new TreeSet<Integer>();
        pod.add(1);
        pod.add(3);
        pod.add(2);
        
        List<Integer> list = new ArrayList<Integer>();
        list.addAll(pod);
    
        for (Integer bean : list) {
            System.out.println(bean);
        }
    }

    3、参考链接



  • 相关阅读:
    DRUPAL-PSA-CORE-2014-005 && CVE-2014-3704 Drupal 7.31 SQL Injection Vulnerability /includes/database/database.inc Analysis
    WDCP(WDlinux Control Panel) mysql/add_user.php、mysql/add_db.php Authentication Loss
    Penetration Testing、Security Testing、Automation Testing
    Tomcat Server Configuration Automation Reinforcement
    Xcon2014 && Geekpwn2014
    phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151
    Linux System Log Collection、Log Integration、Log Analysis System Building Learning
    The Linux Process Principle,NameSpace, PID、TID、PGID、PPID、SID、TID、TTY
    Windows Management Instrumentation WMI Security Technology Learning
    IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
  • 原文地址:https://www.cnblogs.com/deng-cc/p/8072930.html
Copyright © 2011-2022 走看看