问题
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