Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays Vs Linked List

 
vikram hegde
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Java Pals,

Can anyone put some light on, When to use Object[] and when to go for Linked list data structure? Here, Lets assume that the number of Objects to be inserted is known, i.e, let me know other criteria to be considered like performance, memory and any thing other than dynamic growing length property of Linked list over Arrays,in which length must be known at the time of instance creation of array.

with warm regards,
Vikram
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if your numObjects is very large, and say you wanted to acess the last one in the list of objects, think of the time required to do so with a linked list, then with an array.
-just a small thought to try and help
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the only choice -- what about java.util.ArrayList? With the new generics enhancement to collections, I prefer collections like List<Foo> over Foo[], unless I know I must have an array -- perhaps other code is already expecting arrays.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a Crib Sheet I made because I can't keep the pros & cons of all the collections in my head.
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect that the Java implementation of LinkedList hold a pointer to both the head and the tail of the list in order to make getLast fast. Actually, is the implemented LL a double-linked list?

I think the main advantage of a LL over and array is the ability to do fast inserts and deletes. With an array you have to shift elements when you insert or delete.

The crib sheet mentioned in the previous post points this out. I was just trying to mention that getLast() might be optimized.

_M_
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic