java arrayconverter_How to Convert Array to ArrayList in Java? | 学步园

This is a question that is worth to take a look for myself, because it is one of the top viewed and voted questions in stackoverflow. The one who accidentally asks such a question could gain a lot of reputation which would enable him to do a lot of stuff on

stackoverflow. This does not make sense so much for me, but let’s take a look at the question first.

The question asks 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 arrayList = new ArrayList(Arrays.asList(array));

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

ArrayList

- ArrayList(Collection 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”

If the add() method is invoked NOW, the size of the elementData array is not large enough to home one more element. So it will be copied to a new larger array. As the code below indicates, the size grows 1.5 times of old array.

public void ensureCapacity(int minCapacity) {

modCount++;

int oldCapacity = elementData.length;

if (minCapacity > oldCapacity) {

Object oldData[] = elementData;

int newCapacity = (oldCapacity * 3)/2 + 1;

if (newCapacity < minCapacity)

newCapacity = minCapacity;

// minCapacity is usually close to size, so this is a win:

elementData = Arrays.copyOf(elementData, newCapacity);

}

}

2. Next popular answer

The next popular answer is:

List list = Arrays.asList(array);

It is not the best, because the size of the list returned from asList() is fixed. 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.ArrayList

at collection.ConvertArray.main(ConvertArray.java:22)

3. Indications of the question

The problem is not hard, and kind of interesting. Every Java programmer knows ArrayList, it is simple but 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 basically indicate the same solution. This is true for a lot of questions, I guess people just don’t care, they like answering!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值