• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which one is better ....

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have a list of some element and I want to retrieve the elements . So which option is much efficient and good out of this two & why ?





thanks a lot .
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterator is cleaner and easier to read.

Mark
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using an ArrayList, it doesn't matter.
If you are using a LinkList, the Iterator is clearly more effecient.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why so ?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On an ArrayList calling .get(int) is basically an index operation.
On a LinkedList calling .get(int) causes you to walk down the List starting at the first node until you get the the correct index. So you go from a O(1) operation to a O(n^2) operation. was trying to superscript the 2, didn't work

[ February 03, 2005: Message edited by: Steven Bell ]
[ February 03, 2005: Message edited by: Steven Bell ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With Tiger u can use smth. like this:


or even better:

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the best way of itreating the elements in Collection
-------------------------
Collection list = new ArrayList();

Iterator listItr = list.iterator();
int size = list.size();
for(int i = 0;i<size;i++)
{
listItr.next();
}

 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why so ???
 
Bilal Al-Sallakh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steven Bell:
So you go from a O(1) operation to a O(n^2) operation.



I think you he goes from an O(1) operation to an O(n) one.
 
Bilal Al-Sallakh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steven Bell:
So you go from a O(1) operation to a O(n^2) operation.



I think he goes from an O(1) operation to an O(n) one.
 
Bilal Al-Sallakh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
why so ???



You may find a solid data-structures foundation in many data-structures and algorithms book. I think a good java programmer should already be familiar with them.
 
Bilal Al-Sallakh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
why so ???



You may find a solid data-structures foundation in many data-structures and algorithms book. I think a good java programmer should already be familiar with them.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arul Prasad:
the best way of itreating the elements in Collection
-------------------------
Collection list = new ArrayList();

Iterator listItr = list.iterator();
int size = list.size();
for(int i = 0;i<size;i++)
{
listItr.next();
}




why this is best ???
I think this is increasing line of code .
thanks .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic