Answer the question
In order to leave comments, you need to log in
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
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.
What I immediately remembered, although I could be wrong:
- | Arrays | List |
---|---|---|
Change of size | No | Yes |
Element Access Difficulty | always O(1) | depends on List implementation |
Support for read-only semantics | no (workaround for simple cases: array cloning) | yes (using read-only decorator Collections.unmodifiableList(List)) |
"Smooth" integration into the type system | no (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 types | Yes | none ("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 objects | at the time of assigning an array element or its initialization | at the moment of pulling the list element and interpreting it on the side that uses the list |
Mutual transformations | an 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 supported | list 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 elements | Yes | no (because of type erasure; you can only find out the type of an element individually when accessing each element) |
.toString()-representation | none (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-traversal | via a counter to access each element | through an iterator using the implementation of Iterable |
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 questionAsk a Question
731 491 924 answers to any question