zoukankan      html  css  js  c++  java
  • Java中使用Arrays.asList初始化ArrayList

    package xiaoling;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.List;
    public class ListTest{
            public static void main(String[] args){
                    List<List<Integer>> list = new ArrayList<>();
                    for (int num=0; num<10; ++num){
                            list.add(new ArrayList(Arrays.asList(num, num+1)));
                    }
                    System.out.println(list);
            }
    }

     运行结果为:[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10]]

    数组测试:

     1 package xiaoling;
     2 import java.util.Arrays;
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 public class ListTest{
     6     public static void main(String[] args){
     7         List<List<Integer>> list = new ArrayList<>();
     8         for (int num=0; num<5; ++num){
     9             list.add(new ArrayList(Arrays.asList(num, num+1)));
    10         }
    11         System.out.println(list);
    12 
    13         int[] nums = new int[]{11, 12, 13, 14, 15};
    14         list.add(new ArrayList(Arrays.asList(nums)));
    15         System.out.println(list);
    16 
    17         Integer[] nums1 = new Integer[]{11, 12, 13, 14, 15};
    18         list.add(new ArrayList(Arrays.asList(nums1)));
    19         System.out.println(list);
    20     }
    21 }

    [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]
    [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [[I@4defb0be]]
    [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [[I@4defb0be], [11, 12, 13, 14, 15]]

  • 相关阅读:
    Django项目后台不挂断运行
    Django---进阶16<XSS攻击>
    Django---进阶15
    Linux开启MySql远程连接
    Django---进阶14
    Django---进阶13
    微信公众号下载文件(避开微信浏览器的文件下载方法)
    ES5的匿名函数对应ES6的箭头函数
    ES6 数值和布尔值的解构赋值
    ES6--in的用法
  • 原文地址:https://www.cnblogs.com/wangling1820/p/11666522.html
Copyright © 2011-2022 走看看