The Artima Developer Community
Sponsored Link

Java Answers Forum
help using an iterator with a linked list

3 replies on 1 page. Most recent reply: May 5, 2003 6:08 AM by Erik Price

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

help using an iterator with a linked list Posted: May 2, 2003 12:29 PM
Reply to this message Reply
Advertisement
I am having trouble writing an iterator for my linked list could someone help with this.
hasNext();
next();
remove();
Do I need a running temp within the iterator class or what.
thanks.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: help using an iterator with a linked list Posted: May 3, 2003 1:37 PM
Reply to this message Reply
To use an Iterator with a LinkedList to do something a common approach is to use it with a while loop.
The following snippet uses String objects.

java.util.LinkedList myList = new java.util.LinkedList();
myList.add("one");
myList.add("111");
myList.add("222");
myList.add("34ghj");
java.util.Iterator iterator = myList.iterator();
while (iterator.hasNext()){
   String nextListItem = (String)iterator.next();
   System.out.println(nextListItem);
}

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: help using an iterator with a linked list Posted: May 3, 2003 3:08 PM
Reply to this message Reply
I understand that, but I don't get how I should write the code for hasNext() and next()...

Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: help using an iterator with a linked list Posted: May 5, 2003 6:08 AM
Reply to this message Reply
You are writing your own LinkedList then?

Flat View: This topic has 3 replies on 1 page
Topic: don't understand the problem - using methods Previous Topic   Next Topic Topic: Pause in JAVA

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use