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. PhpMyAdmin 配置文件现在需要一个短语密码的解决方法

    新版本的PhpMyAdmin 增强了安全性,需要在配置文件设置一个短语密码.否则进入之后会有“配置文件现在需要一个短语密码.”的红色警叹提示. 解决方法: 1.将 phpMyAdmin/librari ...

  2. 2038: [2009国家集训队]小Z的袜子(hose) 分块

    : [2009国家集训队]小Z的袜子(hose) Time Limit: Sec Memory Limit: MB Submit: Solved: [Submit][Status][Discuss] ...

  3. Linux Linux程序练习四

    编写两个不同的可执行程序,名称分别为a和b,b为a的子进程. 在a程序中调用open函数打开a.txt文件. 在b程序不可以调用open或者fopen,只允许调用read函数来实现读取a.txt文件. ...

  4. [浪风JQuery开发]jquery最有意思的IFrame类似应用--值得深入研究

    前几天一时兴起答应朋友的需求--做一个外国的企业网站: 本想做就做呗,可没想我辛辛苦苦用浪风认真php平台开发后,对方来一句我服务器不能安装其他程序,请给我用frame框架开发. 浪风那是一个苦字难言 ...

  5. 四个 jQuery 方法:

    append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容

  6. ThinkPHP带表情无限级评论回复

    今天就tp中(含表情)无限级评论回复做一个个人总结. 1.准备TP基本框架 2.数据库,数据表的建立 A.先说说数据库(表)的建立. a-1,数据库:blog a-2,数据表:bolg_comment ...

  7. java 接口转码、加密

    备用 package http; import java.io.UnsupportedEncodingException; import sun.misc.*; public class Base64 ...

  8. Fibonacci series(斐波纳契数列)的几种常见实现方式

    费波那契数列的定义: 费波那契数列(意大利语:Successione di Fibonacci),又译费波拿契数.斐波那契数列.斐波那契数列.黄金切割数列. 在数学上,费波那契数列是以递归的方法来定义 ...

  9. jQuery EasyUI编辑DataGrid用combobox实现多级联动

    我在项目中设计课程表的时候需要用到老师和分类之间的多级联动. 首先是一张效果图: 下面是实现的代码: <body> <script type="text/javascrip ...

  10. ChemDraw 15支持哪些输入格式

    当我们想让我们的化学图形应用在试卷编辑.论文撰写.刊物出版等各个方面,这个时候往往都得使用ChemDraw 15.它可以与很多第三方应用灵活.本ChemDraw教程介绍新版ChemDraw Profe ...