A
A
Alex Bezrukov2014-11-17 22:01:08
Java
Alex Bezrukov, 2014-11-17 22:01:08

What is the difference between array and ArrayList in Java?

Good afternoon, ladies and gentlemen. Can someone tell me what is the difference between a regular data array like int[] cats = new int[10] and an ArrayList like ArrayList list = new ArrayList() ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-11-17
@valiofin

int[] cats = new int[10] in memory looks like a contiguous region of memory filled with values ​​and equal to sizeof(int) * 10. Advantages - if you often access and run through a collection of elements, then arrays will provide you with maximum performance only due to index addressing and fewer cache misses (google the data locality property). Cons - fixed size, resizing can be inconvenient and expensive in terms of performance.
ArrayList is a list, just such a list. That is, each element is connected to each other through pointers. Pros - Easy to add and remove items. Cons - elements created at different times may end up in the memory of hell, which can lead to a large number of cache misses. In short, traversing the list is corny slower.
Depending on the task, it makes sense to choose one or another solution.
Updated: In fairness, I'll correct myself. ArrayList is an implementation of a list on arrays, so things are a bit more complicated. The case I described is LinkedList, but the essence is the same - just arrays - fixed size, ArrayList - dynamic size.

H
halogen, 2014-11-19
@halogen

What I immediately remembered, although I could be wrong:

-ArraysList
Change of sizeNoYes
Element Access Difficultyalways O(1)depends on List implementation
Support for read-only semanticsno (workaround for simple cases: array cloning)yes (using read-only decorator Collections.unmodifiableList(List))
"Smooth" integration into the type systemno (arrays are sort of a special case of organizing objects; unlike .NET it doesn't have any array type as its base type)Yes
Support for primitive typesYesnone ("heavy" objects for primitives; slightly offset by the presence of factory methods like Integer.valueOf(int); workaround: FastUtils or Trove)
Checking the type of assigned objectsat the time of assigning an array element or its initializationat the moment of pulling the list element and interpreting it on the side that uses the list
Mutual transformationsan array from a list: List.toArray() always returns a new array of objects of type Object; List.toArray(T[]) allows you to specify a prepared buffer for non-primitives; conversion to an array of primitives is not directly supportedlist from array: Arrays.asList(T...) no problem if the array is an array of objects, not primitive types; if the array is an array of primitives, Arrays.asList() returns a list with one element, considering the input array to be the only object, although there are variants like Ints.asList(int[]) from Guava
Ability to find out the common type of all elementsYesno (because of type erasure; you can only find out the type of an element individually when accessing each element)
.toString()-representationnone (always "[[email protected]" where T is the type of the array, IDENTITY is the unique ID of the object; Arrays.toString() is required)yes (depends on implementation, in general "[n1, n2, n3...]")
Implementing a for-traversalvia a counter to access each elementthrough an iterator using the implementation of Iterable

P
Pavel Gaev, 2014-11-26
@Plohish81

Primitives ( int , long , float , etc.) cannot be stored in an ArrayList . If you add a primitive to the Collection , then autoboxing will occur

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question