Results are out! Find what you Get quick advice or join the chat
Hey there! Sign in to join this conversationNew here? Join for free

hasNext() and next(); method

Announcements Posted on
Let our uni choice search tool match you with your perfect university 19-11-2015
  1. Offline

    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;, I have no idea what hasNext() is for?

    Thanks guys as always!!!! (learning a lot about java from here )
  2. Offline

    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:

          Sausage s = (Sausage)
    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.
  3. Offline

    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:

    Iterator<Object> iterator = list.iterator();
    Object myObject;
    while (iterator.hasNext()) {
        myObject =;
        // 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.
  4. Offline

    You can also use hasNext to test for input ends. Use it in a while loop to scan through user input.
  5. Offline

    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?
  6. Offline

    (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.
  7. Offline

    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.


Submit reply


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?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  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 to join now Processing…

Updated: July 24, 2007
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Today on TSR

Win an iPad Air 2

Don't miss our big TSR competition!

What class are you?
Useful resources
Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.