Hey there Sign in to join this conversationNew here? Join for free

hasNext() and next(); method

Announcements Posted on
TSR Movie Madness is down to only 8 films! Help decide on the final 4 now! 15-07-2014
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    What is the difference between the two methods?

    If I had a sentence and I wanted to scan it; "what is the difference" - I would probably assign the variable to sentence.next();, I have no idea what hasNext() is for?

    Thanks guys as always!!!! (learning a lot about java from here )
    • 0 followers
    Offline

    ReputationRep:
    Are these the methods of Iterator?

    hasNext() tells you if there are any more elements.
    next() iterates to the next element and returns it.

    When you use an iterator to loop though a collection it is usually like this:

    Code:
    while(iter.hasNext())
    {
          Sausage s = (Sausage)iter.next()
    }
    This is what it would be like if you were iterating through a collection of Sausage objects. If the iterator uses generics you use generics you wouldn't need to cast.
    • 0 followers
    Offline

    ReputationRep:
    I presume you mean of the Iterator class? Again if you ask questions about methods say which class they are of as we can only guess what your after. Like I also said last I answered this type of question for you, the answers are in the API. You should keep it bookmarked.

    hasNext() - "Returns true if the iteration has more elements"
    So you can check if there are more objects you can get from the iterator, and if there are it returns true. If there are no more objects it will return false.

    next() - "Returns the next element in the iteration"
    So if there is a next item, it will be returned. If there isn't it'll throw a 'NoSuchElementException', but if you use iterators properly you should never encounter it.

    Essentially you check if there is a variable a next variable with hasNext(), and if so, you get it with next(). Otherwise how else will you know if you are at the end of the list of objects? The two methods are usually used together where instead of using a for loop you can use a while loop, like so:

    Code:
    Iterator<Object> iterator = list.iterator();
    Object myObject;
    while (iterator.hasNext()) {
        myObject = iterator.next();
        // do something with myObject
    }
    In the while loop above I am first checking if the iterator has a next element, and if it does I then store it into a variable and work with it in the while loop. Personally I always prefer for/each loops as it's less code and more straightforward to read. I suspect iterators were a solution to get the functionality of for/each before Java 5. If you don't know them already, I'd recommend researching them next. But you still need to know about iterators.
    • 0 followers
    Offline

    ReputationRep:
    You can also use hasNext to test for input ends. Use it in a while loop to scan through user input.
    • 0 followers
    Offline

    ReputationRep:
    This is sort of "off topic" but is there an Iterator class already built into java? Or do you have to create your own and implement it?
    • 0 followers
    Offline

    ReputationRep:
    (Original post by samicemalone)
    This is sort of "off topic" but is there an Iterator class already built into java? Or do you have to create your own and implement it?
    Iterator is already part of the Java library. It's part of the collections framework.
    • 1 follower
    Offline

    ReputationRep:
    Though it's an interface, not a class, so if you want to iterate over a custom collection, you'll need to implement it yourself.

Reply

Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?

    this is what you'll be called on TSR

  2. this can't be left blank
    this email is already registered. Forgotten your password?

    never shared and never spammed

  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. By joining you agree to our Ts and Cs, privacy policy and site rules

  2. Slide the button to the right to create your account

    Slide to join now Processing…

Updated: July 24, 2007
New on TSR

Talk about SQA results day

Join the chat ahead of grades coming out on Tuesday

Article updates
Useful resources
Reputation gems:
You get these gems as you gain rep from other members for making good contributions and giving helpful advice.