Welcome to Dream.In.Code
Become a Java Expert!

Join 149,755 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,444 people online right now. Registration is fast and FREE... Join Now!




Iterators

 
Reply to this topicStart new topic

Iterators, Basic structure

Roba
20 Feb, 2007 - 05:03 PM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2007
Posts: 2


My Contributions
Hi
I am new to java and have found this forum and it looks to be about the best for constructive help.
I have written a class that generates some random numbers and sorts them but now i am writing another class and have to use import java.util.Iterator
I havent got a clue how it works or even what it should look like and what it actually needs to work.
Is there someone out there that could tell me how it looks/works? I have an array in the first class and the variable for the random number is [i]. How do i import this variable into the second class and use the iterator to output it in the System.out etc etc as many times as the user requires.
It is confusing me to bits. I have found various snippets of code online but it would help if i could actually see a whole class with the iterator in it. I should be able to get it from there.

Hope you can help

Rob
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Iterators
20 Feb, 2007 - 05:23 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Posting some code would help.

Iterator is an interface that gives an object the following methods:
boolean .hasNext()
object .next()
void remove()

Collections have Iterators so if you had a collection called Animals you could get all of the animal names by:
CODE

    Animal a;
    for (Iterator it=Animals.iterator(); it.hasNext(); ) {
        a = it.next();
        System.out.println(a.name)
    }




User is offlineProfile CardPM
+Quote Post

keems21
RE: Iterators
20 Feb, 2007 - 05:26 PM
Post #3

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
Alright, here goes.

An iterator is used to systematically move through some organized set of data. I've never used an iterator for a plain array (i have no idea if it can even be done), but in theory here's how it should work.

There are two useful methods in the iterator class. The first is hasNext(). This returns a boolean value depending on whether or not there is another element in the set of data. It's useful for writing while loops to sort through everything.

CODE

while(iteratorName.hasNext())
{
//check the next value
}


The second useful method is next(). This pretty much returns the next element in your set of data. This is useful because you can set the element equal to a variable, then check to see if that value matches up to the one you're looking for with an if statment.

So here's what you've got so far:
CODE

while(iteratorName.hasNext())
{
  int var1 = iteratorName.next();
  if(var1 == valueYoureSearchingFor)
  {
    //return it, store it, break from the loop, do whatever you need to here
  }
}


I hope this helps you get on your way. Again, I suppose that you can implement this with an array, I've just never done it before. Good luck.
User is offlineProfile CardPM
+Quote Post

Roba
RE: Iterators
21 Feb, 2007 - 12:00 AM
Post #4

New D.I.C Head
*

Joined: 20 Feb, 2007
Posts: 2


My Contributions
You are stars.
It finally makes sense. I had no idea what it was or how it should look/work. Now i have something to get my teeth into and a good idea of how to use it.
Thanks again

Rob
P.S. Think i am going to become a regular...
Cya
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:55AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month