[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Arun Singh Raaj wrote:I want to know, Since ArrayList is backed by array data-structure, do ArrayList elements get stored in contiguous locations similar to array's?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Question: Why do you care?
Arun Singh Raaj wrote:
Winston Gutkowski wrote:Question: Why do you care?
because I want to know why insertion is slower in ArrayList. I understand in case of arrays that to insert a new element it has to shift rest all elements because they are stored in contiguous locations but ArrayList is a dynamic array so I guessed it also stores elements in contiguous locations. please shed some light. thanks
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
It might be interesting to know, but you can program very well without actually knowing those details.Winston Gutkowski wrote:. . . Why do you care? . . .
Yes, and with the exception of booleans, the amount of memory each occupies is defined strictly by the Java® Language. It should therefore always be possible to predict how much space is needed for a primitive in an array.primitives are ... er ... primitive . . .
Arun Singh Raaj wrote:because I want to know why insertion is slower in ArrayList. I understand in case of arrays that to insert a new element it has to shift rest all elements because they are stored in contiguous locations but ArrayList is a dynamic array so I guessed it also stores elements in contiguous locations.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Yes, the documentation link I posted yesterday says,Winston Gutkowski wrote:. . . it increases (and possibly decreases) in size as needed to hold all elements . . .
...but doesn't say anything about shrinking the array, which you can do with this method.Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
Jeanne Boyarsky wrote:Whereas in an ArrayList, I have to update what each index refers to for all subsequent elements. This is true whether tit is stored in an array, a map or even a file mapping indexes to values.
It so happens, the data is stored in an array which you can determine from the source code.
Jeanne Boyarsky wrote:Imagine you have a million elements in your list. Asking for the millionth element of an ArrayList is fast because it goes to that index. However, in a LInkedLIst, Java has to go through each element to get to the last one. Definitely not fast.
source: JavadocOperations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.