Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How data is added in a ArrayList

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Henry Wong
author
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly an advanced question -- moving to JiG, Beginners...

Henry
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh i don't know that , anyway thanks for moving it to appropiate forum .
 
Henry Wong
author
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Henry this is same as hin LinkedList also know then where is the difference ?
 
Henry Wong
author
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was nice , Thank you very much Andre.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic