Just one sec...
Hey there! Sign in to join this conversationNew here? Join for free

hasNext() and next(); method

Announcements Posted on
    • Thread Starter

    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 )

    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)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.

    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 = 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.

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

    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?

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

    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. Oops, you need to agree to our Ts&Cs to register
  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.

Should the UK have more bank holidays?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22

Registered Office: International House, Queens Road, Brighton, BN1 3XE

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