2019独角兽企业重金招聘Python工程师标准>>>

This article analyzes answers for a top-voted questions on Stack Overflow. The person who asked this question got a lot of reputation points, which could grant him permissions to do a lot of things on Stack Overflow. This does not make sense to me, but let's take a look at the question first.

The question is "how to convert the following array to an ArrayList?".

Element[] array = {new Element(1),new Element(2),new Element(3)};

1. Most popular and accepted answer

The most popular and the accepted answer is the following:

ArrayList<Element> arrayList = new ArrayList<Element>(Arrays.asList(array));

First, let's take a look at the Java Doc for the constructor method of ArrayList.

ArrayList(Collection < ? extends E > c) : Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

So what the constructor does is the following:
1. Convert the collection c to an array
2. Copy the array to ArrayList's own back array called "elementData"

Here is the source code of Contructor of ArrayList.

public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length;   if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); }

2. Next popular answer

The next popular answer is:

List<Element> list = Arrays.asList(array);

It is not the best, because the size of the list returned from asList() is fixed. Actually the list returned is not java.util.ArrayList, but a private static class defined inside java.util.Arrays. We know ArrayList is essentially implemented as an array, and the list returned from asList() is a fixed-size list backed by the original array. In this way, if add or remove elements from the returned list, an UnsupportedOperationException will be thrown.

list.add(new Element(4));
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListat collection.ConvertArray.main(ConvertArray.java:22)

3. Another Solution

This solution is from Otto's comment below.

Element[] array = {new Element(1), new Element(2)}; List<element> list = new ArrayList<element>(array.length); Collections.addAll(list, array);

4. Indications of the question

The problem is not hard, but interesting. Every Java programmer knows ArrayList, but it's easy to make such a mistake. I guess that is why this question is so popular. If a similar question asked about a Java library in a specific domain, it would be less likely to become so popular.

There are several answers that provide the same solution. This is also true for a lot of other questions on Stack Overflow, I guess people just don't care what others say if they would like to answer a question!

转载于:https://my.oschina.net/u/138995/blog/304475

How to Convert Array to ArrayList in Java?相关推荐

  1. java中Array和ArrayList区别

    2019独角兽企业重金招聘Python工程师标准>>> 1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array". 2)Array([] ...

  2. java中Array和ArrayList区别 可以将 ArrayList想象成一种会自动扩增容量的Array

    java中Array和ArrayList区别 1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array https://blog.csdn.net/ywjy10280 ...

  3. Java中Array和ArrayList之间的9个区别

    array和ArrayList都是Java中两个重要的数据结构,在Java程序中经常使用. 即使ArrayList在内部由数组支持,了解Java中的数组和ArrayList之间的差异对于成为一名优秀的 ...

  4. Java ArrayList的Array,Array的ArrayList

    Today we will learn how to create a Java array of ArrayList. We will also learn how to create an Arr ...

  5. 如何在Java中使用Array实现ArrayList

    ArrayList is the most popular implementation of List in java. ArrayList是java中List的最受欢迎的实现. ArrayList ...

  6. array,arraylist,string的总结

    import java.util.ArrayList;/* * 这个程序是对array,arraylist,string的总结 * 1.创建 *(1)数组:分为动态初始化和静态初始化.(动态初始化时其 ...

  7. 基础回顾(正则、循环、和、 int和Integer、String /StringBuffer、Array和ArrayList、值传递和引用传递、 Lamda、java8)

    1. 正则表达式 1.1 Java中是如何支持正则表达式操作的? Java中的String类提供了支持正则表达式操作的方法,包括:matches().replaceAll().replaceFirst ...

  8. mybatis中传集合时 报异常 invalid comparison: java.util.Arrays$ArrayList and java.lang.String

    mybatis中传集合时 报异常 invalid comparison: java.util.Arrays$ArrayList and java.lang.String 参考文章: (1)mybati ...

  9. Array和ArrayList区别

    Array和ArrayList区别 Array 在C#中的数组也是对象,实际上是Array类的实例. 主要区别 Array类型的变量在声明的同时,必须进行实例化,至少是初始化数组的大小,而 Array ...

最新文章

  1. antd 验证 动态 required_3分钟短文:十年窖藏,Laravel告诉你表单验证的正确姿势
  2. AI 综述专栏 | 超长综述让你走近深度人脸识别
  3. 处理字符数据--运算符和函数
  4. OS_CORE.C(总结)
  5. symbian 视频播放解决方案
  6. Core官方DI解析(2)-ServiceProvider
  7. s5-2 Cpu调度算法
  8. 适用于Java开发人员的Elasticsearch:Elasticsearch生态系统
  9. Android8有深度休眠吗,IMX8MQ android休眠功耗过大
  10. python数据结构的列表_Python自带数据结构 列表(list)
  11. 高德软件有限公司python试题 及 答案
  12. Android之本地摄像头,Android之调用本地摄像头
  13. python是什么 自学-你们都是怎么自学python的?
  14. manjaroLinux下安装mysql时初始化mysql出错解决办法
  15. 【手写数字识别】基于matlab CNN网络手写数字识别分类【含Matlab源码 1286期】
  16. Delphi2007,XE2 调用GetVersionEx在Win10下为6.1的原因及解决方法
  17. 谷歌这是要全面退出中国!
  18. Accelerate Activity Tracker隐私政策
  19. 哪个软件能准确测试人脸,人脸识别软件哪个好?人脸识别软件推荐2020
  20. 如何选购笔记本电脑?

热门文章

  1. 刚刚!刘永坦院士和钱七虎院士荣获2018年度国家最高科技奖
  2. 手术革命:这三家公司如何用AR技术辅助医疗手术
  3. 下一版Win10,微软人工智能平台Windows ML要和你见面了
  4. 通过外貌就能看出程序员的资历? | 每日趣闻
  5. 如果宁静是 Oracle,万茜、张雨绮、黄圣依是什么?
  6. 10个随机数相加等于100
  7. 临时表空间过大解决方法
  8. JSONObject和JSONArray(json-lib-2.4)的基本用法
  9. 管理云栈将会胜出吗?
  10. 【鲁班学院】一个三年工作经验和月薪16k的java程序员应该要具备什么样的技能?...