String s[]={"aa","bb","cc"};
List<String> sList=Arrays.asList(s);
for(String str:sList){//能遍历出各个元素
System.out.println(str);
}
System.out.println(sList.size());//为3 System.out.println("- - - - - - - - - - -"); int i[]={11,22,33};
List intList=Arrays.asList(i); //intList中就有一个Integer数组类型的对象,整个数组作为一个元素存进去的
for(Object o:intList){//就一个元素
System.out.println(o.toString());
} System.out.println("- - - - - - - - - - -"); Integer ob[]={11,22,33};
List<Integer> objList=Arrays.asList(ob); //数组里的每一个元素都是作为list中的一个元素
for(int a:objList){
System.out.println(a);
} System.out.println("- - - - - - - - - - -"); //objList.remove(0);//asList()返回的是arrays中私有的终极ArrayList类型,它有set,get,contains方法,但没有增加和删除元素的方法,所以大小固定,会报错
//objList.add(0);//由于asList返回的list的实现类中无add方法,所以会报错

 输出结果:

  1. aa
  2. bb
  3. cc
  4. 3
  5. - - - - - - - - - - -
  6. [I@287efdd8
  7. - - - - - - - - - - -
  8. 11
  9. 22
  10. 33
  11. - - - - - - - - - - -

如果想根据数组得到一个新的正常的list,当然可以循环一个一个添加,也可以才有以下2个种方法:

 ArrayList<Integer> copyArrays=new ArrayList<Integer>(Arrays.asList(ob));//这样就是得到一个新的list,可对其进行add,remove了
copyArrays.add(222);//正常,不会报错 Collections.addAll(new ArrayList<Integer>(5), ob);//或者新建一个空的list,把要转换的数组用Collections.addAll添加进去

 

另外讨论下Collections.addAll和list.addAll

 List<Integer> list1 = new ArrayList<Integer>() {{add(0); add(-1);}};
List<Integer> list2 = new ArrayList<Integer>(Arrays.asList(2, 4, -9)); list1.addAll(list2);
System.out.println(list1);
list2.set(0, 100000);
System.out.println(list1);//深复制
System.out.println(); //功能性展示
Collections.addAll(list2, 34, 67, 78);
System.out.println(list2);
list2.addAll(Arrays.asList(34, 67, 78));
System.out.println(list2);
System.out.println();

结果:

[0, -1, 2, 4, -9]
[0, -1, 2, 4, -9]

[100000, 4, -9, 34, 67, 78]
[100000, 4, -9, 34, 67, 78, 34, 67, 78]

 

Array.asList:数组转list的更多相关文章

  1. Array.asList:数组转list时你一定要知道的“陷阱”!

    最近开发中,业务上处理,经常用到asList方法,这让我不经想起了它的很多容易让人犯错的地方或者误解的地方,所以就想抽出时间来,整理一下,和大家分享出来,深夜了,话不多说,主要以代码为主,简易的代码, ...

  2. Arrays.asList(数组) 解说

    最近在用Arrays的asList()生成的List时,List元素的个数时而不正确. Java代码 一:Arrays.asList(数组)该方法是将数组转化为集合(该方法主要用于Object对象数组 ...

  3. golang之 Array(数组)

    目录 一.Array(数组) 二.数组的定义 1. 基本语法 三.数组的初始化 1. 方式一 2. 方式二 3. 方式三 四.数组的遍历 1. 方式一:for循环遍历 2. 方式二:for range ...

  4. vector以及array和数组

    //比较数组.vector.array #include <iostream> #include <vector> #include <array> #includ ...

  5. java中Array(数组)的用法

    8.Array(数组)    数组是作为对象来实现的.(really occupy the memopry,真实的占用内存 ) An array is a data structure that st ...

  6. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

  7. [LeetCode] Patching Array 补丁数组

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  8. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  9. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. 华为终端开放实验室Android Beta 4测试能力上线

    ​​​7月26日,Android P Beta 4发布(即Android P DP5),此版本为开发者最后一个预览版本,也预示着Android P正式版即将与大家见面. 为保证开发者在正式版本来临前做 ...

  2. 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟

    /** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...

  3. Who's in the Middle - poj 2388 (快速排序寻找中位数)

    题意; 寻找中位数 利用快速排序来寻找中位数. #include <iostream> using namespace std; int N; ]; int Median(int left ...

  4. 解决Bootstrap 试用手机端 布满全屏

    @media (max-width: 767px) { body{ margin: 0; padding: 0; } } @media (max-width: 970px) { body{ margi ...

  5. day20常用模块

    一.正则内容的补充 import re # ret = re.findall(r'www\.baidu\.com|www\.oldboy\.com','www.baidu.com') # # ret ...

  6. TP ajax

    ①Ajax使用:   注意传值的所有过程用的是小写,及时数据库列的名称中有大写字母 控制器部分: AjaxController.class.php <?php namespace Home\Co ...

  7. CodeIgniter框架——访问方式 URI 分配变量 数据库操作

    1.访问方式: CodeIgniter 的访问URL使用的是pathinfo,入口文件/控制器/方法(/参数列表) eg:localhost/index.php/welcome/index/id 第一 ...

  8. [Spring MVC]学习笔记--@RequestMapping支持的返回类型

    下面针对官方文档列出的支持类型进行举例. (本篇例子存于github上, https://github.com/lemonbar/spring-mvc-requestmapping) 可以直接下载, ...

  9. Cocos2d-x Lua中使用标签

    游戏场景中的文字包括了静态文字和动态文字.静态文字如下图所示游戏场景中①号文字“COCOS2DX”,动态文字如图4-1所示游戏场景中的②号文字“Hello World”.静态文字一般是由美工使用Pho ...

  10. event.preventDefault方法的使用

    event.preventDefault()方法是用于取消事件的默认行为,例如,当点击提交按钮时阻止对表单的提交.但此方法并不被ie支持,在ie下需要用window.event.returnValue ...