Arraylist and Arrays

故事扮演 提交于 2020-01-07 08:09:15

问题


What is the difference between

ArrayList<Object> al = new ArrayList<Object>(100);

and

Object[] ar = new Object[100];

Is there any difference in the internal implementation i.e. the allocation in the memory? Do both internally reserve 100 slots in the memory?


回答1:


An ArrayList<Object> holds a backing Object[]. The backing Array will be "resized" (a new array will be created and the old data will be copied over) if you would possibly overflow the size and additions are possible.

List implementations also provide additional methods to work on them.

Another thing is that you may use Generics(1.5) with Lists.




回答2:


ArrayList is a List-implementation that uses a normal array internally to store and retrieve the objects added to list. The latter (Object[]) is a normal array.




回答3:


No, both does not mean same in ArrayList < Object > al = new ArrayList< Object >(100); just define initialCapacity in later you can add n number of elements but in Object[] ar = new Object[100]; you cannot add elements after 100 size




回答4:


ArrayList can grow or shrink dynamically at runtime, arrays cannot. ArrayList is a list implemented using an array. It gives you a variety of functions (list operations basically) which array doesn't provide.



来源:https://stackoverflow.com/questions/8937560/arraylist-and-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!