Re: Size of an arraylist in bytes
On 11/20/2011 4:01 PM, sara wrote:
Hi All,
I create an Arraylist<Integer> tmp and add some integers to it.
Afterward, I measure the size of tmp in bytes (by converting tmp to
bytes array). Assume the result is byte[] C. However, when I update an
element of tmp, and measure size of tmp in bytes again, the result is
different than C!
Why this is the case?
See markspace's response. Another possible point of confusion:
The ArrayList does not actually contain objects, but references to
those objects -- that's why the same object instance can be in three
ArrayLists, two Sets, and a Map simultaneously. In fact, the same
Integer object could appear forty-two times in a single ArrayList:
List<Integer> list = new ArrayList<Integer>();
Integer number = Integer.valueOf(42);
for (int i = 0; i < 42; ++i)
list.add(number);
If you're coming from a C background, a rough analogy is that
the ArrayList holds "pointers" to the objects it holds, not copies
of those objects.
--
Eric Sosman
esosman@ieee-dot-org.invalid