How to Convert Array to ArrayList in Java?
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?".
|
1. Most popular and accepted answer
The most popular and the accepted answer is the following:
|
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.
|
2. Next popular answer
The next popular answer is:
|
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.
|
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.
|
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?相关推荐
- java中Array和ArrayList区别
2019独角兽企业重金招聘Python工程师标准>>> 1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array". 2)Array([] ...
- java中Array和ArrayList区别 可以将 ArrayList想象成一种会自动扩增容量的Array
java中Array和ArrayList区别 1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array https://blog.csdn.net/ywjy10280 ...
- Java中Array和ArrayList之间的9个区别
array和ArrayList都是Java中两个重要的数据结构,在Java程序中经常使用. 即使ArrayList在内部由数组支持,了解Java中的数组和ArrayList之间的差异对于成为一名优秀的 ...
- 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 ...
- 如何在Java中使用Array实现ArrayList
ArrayList is the most popular implementation of List in java. ArrayList是java中List的最受欢迎的实现. ArrayList ...
- array,arraylist,string的总结
import java.util.ArrayList;/* * 这个程序是对array,arraylist,string的总结 * 1.创建 *(1)数组:分为动态初始化和静态初始化.(动态初始化时其 ...
- 基础回顾(正则、循环、和、 int和Integer、String /StringBuffer、Array和ArrayList、值传递和引用传递、 Lamda、java8)
1. 正则表达式 1.1 Java中是如何支持正则表达式操作的? Java中的String类提供了支持正则表达式操作的方法,包括:matches().replaceAll().replaceFirst ...
- mybatis中传集合时 报异常 invalid comparison: java.util.Arrays$ArrayList and java.lang.String
mybatis中传集合时 报异常 invalid comparison: java.util.Arrays$ArrayList and java.lang.String 参考文章: (1)mybati ...
- Array和ArrayList区别
Array和ArrayList区别 Array 在C#中的数组也是对象,实际上是Array类的实例. 主要区别 Array类型的变量在声明的同时,必须进行实例化,至少是初始化数组的大小,而 Array ...
最新文章
- antd 验证 动态 required_3分钟短文:十年窖藏,Laravel告诉你表单验证的正确姿势
- AI 综述专栏 | 超长综述让你走近深度人脸识别
- 处理字符数据--运算符和函数
- OS_CORE.C(总结)
- symbian 视频播放解决方案
- Core官方DI解析(2)-ServiceProvider
- s5-2 Cpu调度算法
- 适用于Java开发人员的Elasticsearch:Elasticsearch生态系统
- Android8有深度休眠吗,IMX8MQ android休眠功耗过大
- python数据结构的列表_Python自带数据结构 列表(list)
- 高德软件有限公司python试题 及 答案
- Android之本地摄像头,Android之调用本地摄像头
- python是什么 自学-你们都是怎么自学python的?
- manjaroLinux下安装mysql时初始化mysql出错解决办法
- 【手写数字识别】基于matlab CNN网络手写数字识别分类【含Matlab源码 1286期】
- Delphi2007,XE2 调用GetVersionEx在Win10下为6.1的原因及解决方法
- 谷歌这是要全面退出中国!
- Accelerate Activity Tracker隐私政策
- 哪个软件能准确测试人脸,人脸识别软件哪个好?人脸识别软件推荐2020
- 如何选购笔记本电脑?