Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 06:31 AM
Member
 
Join Date: Jul 2007
Posts: 40
lenny is on a distinguished road
How to use an Iterator in java
Hi, My question is on how to use an Iterator in java. I understand that it is an interface with three methods, but someone said to me that Iterator is also a method in the Iterator class file. After defining the hasNext(), etc. methods, how would I use the iterator and define something of that type? I've tried to look up information on iterators, but I haven't found a good explanation of it anywhere.
Using various user commands, we are to add words to the list (keeping the list in abc order), remove words from the list, print the current list, and check if a certain word is in the list. I have to use iterator in this program.

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2007, 02:03 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
I think you have slightly misunderstood the iterator method as this is not on the Iterator interface but is actually on the Collections which use the iterator.

When obtaining an iterator from a Collection you would call the iterator() method which gives you an iterator of the correct type.

In order to use your own iterator for your collection you would tend to implement the Collection interface (or simply extend an existing one) and then override the iterator method to return your custom version.

You could then chuck your objects into your custom collection and acquire the iterator and voila.

Hope this helps.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-25-2007, 09:46 PM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
Here's a quick example:

ArrayList<String> al = new ArrayList<String>();
al.add("one");
al.add("two");

Iterator i = al.iterator();
while (i.hasNext()) {
String temp = (String) i.next();
System.out.println(temp);
}

============
one
two

Note: I put a string into the array but you can put any object of any type and iterate through it.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
iterator issues orchid New To Java 2 08-12-2008 03:43 PM
Iterator eva New To Java 0 01-31-2008 04:07 PM
using Iterator with Vector Java Tip Java Tips 0 11-13-2007 12:52 PM
Implementing Iterator Harb New To Java 6 08-05-2007 04:24 AM


All times are GMT +3. The time now is 08:12 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org