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
Joined: Oct 11, 2004
Posts: 3829
posted
0
Why so ?
Steven Bell
Ranch Hand
Joined: Dec 29, 2004
Posts: 1071
posted
0
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 ]
Sergey Sytnik
Greenhorn
Joined: Feb 03, 2005
Posts: 1
posted
0
With Tiger u can use smth. like this:
or even better:
Arul Prasad
Ranch Hand
Joined: Jan 20, 2005
Posts: 57
posted
0
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(); }
With Regards<br />Arul
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
posted
0
why so ???
Bilal Al-Sallakh
Greenhorn
Joined: Jan 28, 2005
Posts: 20
posted
0
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
Joined: Jan 28, 2005
Posts: 20
posted
0
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
Joined: Jan 28, 2005
Posts: 20
posted
0
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
Joined: Jan 28, 2005
Posts: 20
posted
0
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
Joined: Oct 11, 2004
Posts: 3829
posted
0
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 .