Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an ArrayList an Array or a List?

Simple old school C# question: Is an ArrayList an Array or a List? The differences between the two are enormous, so I'm curious if anyone knows the way that ArrayLists store data?

like image 326
sircodesalot Avatar asked Aug 05 '13 04:08

sircodesalot


People also ask

What is difference between ArrayList and List?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.

Is a List the same as an array in Java?

In Java, all collections store only references to objects, not the objects themselves. Both arrays and ArrayList will store a few thousand references in a contiguous array, so they are essentially identical.


1 Answers

ArrayList behaves like a list (in particular, its count of elements can grow, unlike an array), but it's backed by an array, which will be dynamically resized as needed. Hence the name ArrayList.

Things that behave like lists don't have to be backed by arrays (they could be linked lists, for example), but ArrayList is.

like image 67
jason Avatar answered Sep 20 '22 06:09

jason