Results 1 to 7 of 7
Thread: efficiency question
- 04-24-2012, 07:57 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
- 04-24-2012, 08:10 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: efficiency question
Give us a whole example. The numbers are already in the list? You only read from the list? If, in which way?
Do you understand the differents between arraylist and linkedlist implementations?
- 04-24-2012, 08:48 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: efficiency question
Finding element #n in an ArrayList takes one single operation while it takes n operations for a LinkedList (it's all in the API documentation).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2012, 12:07 AM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
- 04-25-2012, 12:15 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: efficiency question
newbie here; I think an array can reference by a[i] so you directly go to that element that is required....with a linked list there is no referencing the i'th element so every time you do an operation it starts from the beginning of the linked list and traverses through the list until it finds the element that it needs, that's why takes much longer.............so if you have 100k elements, linked list has to go through each one to reach the element that you might want. that's what i assume
- 04-25-2012, 12:47 AM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
Re: efficiency question
// count even numbers in a sequence of integers public static int countEven(List<Integer> seq) {
int c = 0; for (int i = 0; i < seq.size(); i++) {
if ( seq.get(i) % 2 == 0 ) { c++; }
} return c;
//fill up the array and linkelist seqArr is the arraylist and seqLL is the linkedlist.
int n = Integer.parseInt(args[0]);
for (int i = 0; i < 400000; i++) {
int n = (int) (Math.random() * 100);
seqArr.add(n);
seqLL.add(n);
Than I use the method countEven on both of them.
Hopefully this clears it up. Why does the array come back with the answer quicker?
- 04-25-2012, 02:55 AM #7
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: efficiency question
According to the API documentation for LinkedList, it implements a doubly linked list. When you use indices to access entries, the implementation starts at the head or tail of the list, whichever is closer, and must traverse the list to get to the desired index. That traversal is what you are experiencing as an extended elapsed time to completion compared to the direct access that the ArrayList implementation affords you.
Similar Threads
-
efficiency problem
By marcosol in forum Advanced JavaReplies: 3Last Post: 03-10-2012, 12:41 PM -
Efficiency of Searching
By fam2315 in forum New To JavaReplies: 12Last Post: 08-03-2011, 08:44 AM -
Cryptography Efficiency
By joshdgreen in forum Advanced JavaReplies: 22Last Post: 10-29-2010, 09:16 AM -
question about connection efficiency in database
By mr_anderson in forum JDBCReplies: 7Last Post: 07-29-2010, 11:25 AM -
An Efficiency Question
By Revenna in forum Java 2DReplies: 0Last Post: 06-25-2010, 07:22 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks