Traversing a LinkedList in Java

redundant

Newbie
Joined
May 8, 2004
Messages
349
Reaction score
0
Hey,

Just wondering if anyone could help me out here. I didn't have much luck over at codingforums.com, so if anyone knows any good forums that have a good response rate that'd help me out heaps too...

Anyways, back to the matter at hand. I've got a LinkedList in a Java Class that I'm making and I basically need to go through each item in the list (an object) and apply a method to each of them. Right now my code works, but I'm doing it the same way that i would if it were a normal array - which I know is hell slow for a LinkedList. That is: -
Code:
for (int i = 0; i<list.size(); i++)
{
   apply method to list item here;
}
I know that there's a far better way to do this for a linkedList, but I just can't remember. Any help would be much appreciated.
 
Use a list iterator, its usually the easiest and fastest way to do list traversal.

Code:
Iterator it = list.listIterator();
while(it.hasNext()) {
  Object data = it.next();
  //do something with the data
}

The other way is to use the method list.get(i), but that is slower since the internal code must use an iteration to get each one. The iterator holds a reference to your current location in the list, but using the get method means you have to go the first, then the second... up to the nth element each time and runs much slower.

If you are planning on using a small list or are just trying it out using the get method is fine since you wont notice a performance difference on small amounts of data, but using an iterator is good practice for real applications.
 
=0 Another vigilante!

What are the names of the methods you are applying, and to what types of objects, and what are the methods doing? I have entire singly and doubly linked classes on my pc if you want them. I have a singly linked list attatched. (Don't know how much good it will do you)
 

Attachments

  • linkedList.txt
    3.6 KB · Views: 363
:p :p
Vigilante said:
=0 Another vigilante!

What are the names of the methods you are applying, and to what types of objects, and what are the methods doing? I have entire singly and doubly linked classes on my pc if you want them. I have a singly linked list attatched. (Don't know how much good it will do you)

The Java API comes with a linked list class so it's unneccessary to make your own. All the code i posted above assumes that you are using the java API LinkedList class (LinkedList list = new LinkedList())

And im the original vigilante :p
 
Thanks heaps guys. That was exactly what I was after, VigilanteP.

It was for a uni project I'm making that simulates students using a computer lab. The linked list is used as a queue of students waiting for a free computer to become available. I needed to traverse the list so that I can call a method that updates each students' 'timeWaited' instance variable every time the program loops through another time step.

Maybe now it won't be so slow when I've got 2000 students and 5 computers! Thanks again guys.
 
VigilanteP said:
:p :p

The Java API comes with a linked list class so it's unneccessary to make your own. All the code i posted above assumes that you are using the java API LinkedList class (LinkedList list = new LinkedList())

And im the original vigilante :p
I'm not too sure about the "Original Vigilante", but I know that Java does have a built in linked list class, it was our assignment to make our own in AP Comp. Sci, though. It helped me understand how they work (somewhat similar to array lists).
 
Back
Top