How ArrayList will be stored in Memory? (Conitguously or not?)
Hi,
I have a doubt on arraylists. I would like to know, how arraylist elements will be stored in memory. Will they be stored in contiguous memory locations or not? Also Can anyone, please tell me the difference between ArrayList and Vector in terms of Size and Capasity, the use?
Thanks in advance,
Ravindranath Y
Arraylist
Arrays memory must be alloacted contiguously. ArrayList uses array of Object class to store all its elements internally. Event though the real elements, which are objects to be stored can be allocated space anywhere on heap the refernces that will refer to all of them must have contiguous memory. As long as OS is able to serve JVM's contiguos (big) memory request fast your application runs fast. Since you are adding and removing elements from arrayList in bulk , there is a lot of array copying going on inside placing frequent memory allocation demands. In fact, the performance of your application depends on many factors beyond your control. Most of them are memory allocation/delocation. If you use linked list then memory allocation will be fast because it doesn't have to be contigous and in bulk.
Arraylist is roughly equivalent to Vector, except that it is unsynchronized
Arraylists are implemented with arrays, but you don't need to worry about where in memory they are stored. The only thing that matters to the programmer is how efficient things like inserting, deleting, and moving elements are. If you notice a performance problem with ArrayLists, then do some profiling. LinkedLists store their elements in linked objects, so it may be a better fit for your application. But unless you have a performance problem, don't worry about it. (This is a good reason to program to the List interface instead of ArrayList, it makes it easier to switch implementations later if needed).
Vector and ArrayList are mostly the same, except Vector is automatically synchronized, which makes it slower. Use ArrayList instead, and if you need it to be synchronized, use the Collections.synchronizedList() method to create one.
Hi,
Thanks for the reply, I want to know the size and capasity variance between Vectors and Arraylists. Like, what is the default size of an arraylist and a vector? What happens if that default size limit exceeds? etc..
And most importantly, where can i find this type of in depth information about vectors or arraylists what ever it may be? Please suggest...
Thanks,
Ravindranath Y
You can read all about the size and capacity increments and what happens when the size is exceeded in the api for those classes. Unless you're working with legacy code though, you should be using ArrayList only, so don't worry about Vector.
> And most importantly, where can i find this type> of in depth information about vectors or arraylists> what ever it may be? Please suggest...> In a file called src.zip on your harddrive: it contains the source code for the API classes.
If the API doesn't answer some of these questions, for instance the API for ArrayList states:
<quote>
As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
</quote>
Then it's not nailed down -- implementations are free to handle growth differently.
Often I like to read the source code, you should have downloaded src.zip with the SDK, but realize that it is chock full of implementation choices, of course.
> If the API doesn't answer some of these questions,
> for instance the API for ArrayList states:
>
> <quote>
> As elements are added to an ArrayList, its capacity
> grows automatically. The details of the growth policy
> are not specified beyond the fact that adding an
> element has constant amortized time cost.
> </quote>
>
> Then it's not nailed down -- implementations are free
> to handle growth differently.
>
> Often I like to read the source code, you should have
> downloaded src.zip with the SDK, but realize that it
> is chock full of implementation choices, of course.
I think that when details like that are left out of that api, then most of the time they aren't important enough to need to know. You can use an ArrayList just fine without knowing the growth policy. If you're thinking about that, then you're probably thinking about optimization (Stop That!). I'm not saying it not worth checking out, but it's more of an academic question than a practical one.
Hi Guys,
Thanks for the prompt responses, But my doubt was not clarified completely. A simple question, which one of these Collections is better? (lets say, i am not bothering about Synchronization)
My Program may need to add and remove elements or objects whatever they may be, dynamically. Should i use Arraylist or Vector? what happens if the default size exceeds in both cases? What is the difference between Size and Capasity in Arraylist or Vector? Hope you guys, understand my doubt.
Thanks,
Ravi
> A simple question, which one of these Collections is better? In all conditions? An impossible question to answer.Which should be you default choice? ArrayList.Vector is mainly hanging around for legacy reasons.
> You can read all about the size and capacity
> increments and what happens when the size is exceeded
> in the api for those classes. Unless you're working
> with legacy code though, you should be using
> ArrayList only, so don't worry about Vector.
Swing has lots of classes methods that only take Vectors.
Kaj
> Hi Guys,
> Thanks for the prompt responses, But my doubt
> was not clarified completely. A simple question,
> which one of these Collections is better? (lets say,
> i am not bothering about Synchronization)
Did you read replies 2 and 4? Because I already answered that twice.
> My Program may need to add and remove
> elements or objects whatever they may be,
> dynamically. Should i use Arraylist or Vector? what
> happens if the default size exceeds in both
> cases? What is the difference between Size and
> Capasity in Arraylist or Vector? Hope you guys,
> understand my doubt.
When the size is exceeded, it increases the size automatically. You don't need to care about how it does it, just know that it's done for you. If you really want to know exactly what happens, then read the source code like The Good Dr. pointed out.
> Arrays memory must be alloacted contiguously.Must? The VM hides those implementation details. The memory used might not be contiguous.Kaj
> I think that when details like that are left out of that api, then most of the time
> they aren't important enough to need to know. You can use an ArrayList just fine
> without knowing the growth policy. If you're thinking about that, then you're
> probably thinking about optimization (Stop That!). I'm not saying it not worth
> checking out, but it's more of an academic question than a practical one.
I agree with all that, but I recommend reading the source code for more than
academic interest. I like to know what's going on under the hood; I like to
see that it's ordinary code, not superhuman exertions or magic pixie dust;
I sometimes get ideas about how to do things; and sometimes it does answer
questions that I couldn't find in the API. You have to be careful with that last point,
of course...
> > You can read all about the size and capacity
> > increments and what happens when the size is
> exceeded
> > in the api for those classes. Unless you're
> working
> > with legacy code though, you should be using
> > ArrayList only, so don't worry about Vector.
>
> Swing has lots of classes methods that only take
> Vectors.
>
> Kaj
That's what I meant, although I guess legacy code isn't really a good description of swing :). How about this: Unless you're working with an api that requires Vector, you should be using ArrayList.
I think all your answers lay in the javadocs and source code for Vector and ArrayList.
Hi Guys,
Just now, i got this from one of my collegues. He is saying that the default size allocated for an arraylist is 10, whereas it is 20 for Vector.
And if the default size is exceeded, vector would doubles the size and the arraylist would allocates memory as half the size.(likeif default is 10, it allocated 5 more then again if it exceeds, it allocated 15/2 ie 7 more blocks etc... ) Is this Correct? Does anybody have clear idea on this.
Why i am stressing on this is i faced these in depth questions in some interview. Hope u got my point.
Thanks.
Ravi.
Your colleague is wrong. You can find the right answer in the APIs.If I were interviewing someone, I would find the skill of beingwilling and able to find an answer in the APIs preferable to having crammed and memorized a few answers.