This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes How data is added in a ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How data is added in a ArrayList" Watch "How data is added in a ArrayList" New topic
Author

How data is added in a ArrayList

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hello ,

This is all with respect to LinkedList

In linked list data is stored in nodes that have reference to the previous node and the next node so adding element
is simple as creating the node an updating the next pointer on the last node and the previous pointer on the new
node. so Addition and Deletion in linked list is fast

But can anybody pleease tell me

how data is added in a ArrayList .


Save India From Corruption - Anna Hazare.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Not exactly an advanced question -- moving to JiG, Beginners...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

oh i don't know that , anyway thanks for moving it to appropiate forum .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Ravi Pavan wrote:
how data is added in a ArrayList .


I am assuming that you mean how does the arraylist add an element into the array that it is using internally to hold the elements.... Basically, elements are moved in the array. To insert into location N, the elements at N to the logical end is moved by one, so that the new element can be inserted.

If moving the elements will cause an overflow, then a new (array) is created, and the elements are copied to the new array.

Henry
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

hey Henry this is same as hin LinkedList also know then where is the difference ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Ravi Pavan wrote:hey Henry this is same as hin LinkedList also know then where is the difference ?


Can you care to elaborate why you think the implementation of a linked list and the implementation of an array list are the same?

Henry
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Hi!

Try to code your own class of ArrayList (I'm not saying that you should use it - just code for fun and to know how the ArrayList class could work). Use in it a generic array (like T[]). When you add something, you create a new generic array with the size plus one. When you remove an element, you can adjust the size and the elements of the array (not creating a new one) and keep an index pointer.

If that's not the right way to code a new array class, why don't you try to look to the source code of ArrayList.java?

Sorry my english
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

so i think that

In linked list data is stored in nodes that have reference to the previous node and the next node and this is not there in ArrayList .

Am i right ?
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Ravi Pavan wrote:so i think that

In linked list data is stored in nodes that have reference to the previous node and the next node and this is not there in ArrayList .

Am i right ?

Yeah, you can say so.

This code below is something like LikedList (I believe so):

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

That was nice , Thank you very much Andre.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How data is added in a ArrayList