Java - ListIterator and hasNext

I am learning Java and I have a problem with ListIterator . I have a list with these characters: buong i orn o. My code returns “buongiorno”, while I expected it to print “buongiorn”, without the final “o”. I was expecting this because of the hasNext() function. My code uses recursion. Can you explain the reason for me?

 public static String creaStringa(List<Character> lista) { System.out.println(lista.size()); ListIterator<Character> it = lista.listIterator(); return ricCrea(it); } public static String ricCrea(ListIterator<Character> it) { if(!(it.hasNext())) { return ""; else return String.valueOf(it.next()) +ricCrea(it); } 
+4
source share
5 answers

It would be clearer if there was only one element in the list, say, “b”. hasNext() will indeed return true, and next() will read it, and the iteration will end after that.

Explanation:

If you call Iterator<Object> it= list.iterator() on any non-empty list (even if it has only one item), you get true to call hasNext() . This is because the iterator is initialized BEFORE the first element:

  bungiorno ^ i - iterator 

And when you call next() , it does two things:

  • it reads the element before the iterator,
  • moves the iterator immediately after the element that has just been read, and until the next.

In your example, it prints "b" and stops before "u":

  bungiorno ^ i 

And just before the end:

  bungiorno ^ i 

In fact, it has the following meaning - "o". A call to next() will read this value and jump after o . No more items. hasNext() display false, and calling next() will throw an exception.

Technical details:

The basic idea of ​​implementing an iterator is as follows: - when an Iterator is created by calling iterator() on a List , its internal variable called next points to the first element of the list. - hasNext() just checks to see if next != null . - next() returns next and sets next to display the next element.

This is the java.util.ArrayList Iterator (omitted with some details):

 public Iterator<E> iterator() { return new Itr(); } private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } public E next() { checkForComodification(); int i = cursor; Object[] elementData = ArrayList.this.elementData; cursor = i + 1; return (E) elementData[lastRet = i]; } } 
+5
source

ListIterator.hasNext() will return true for the last character "o" (like any other character). Therefore, the else command will also be executed. It is only after the last "o" has been selected that hasNext() will return false (since now all the elements have passed).

It should be noted here that hasNext() checks to see if there is anything else to return when next() called. It does not extract and does not pass it by itself.

+1
source

The iterator begins to "point" to the element before the first.

next () causes the iterator to point to the next element and return it.

So, you are actually sorting through the whole list.

This means that the following syntax is executed during retry:

  while (it.hasNext ())
 {
    ItemClass itemValue = it.next ();
    // do something with the value
 }

the same syntax can be used even if the iterator is empty.

+1
source

When the iterator is at the last 'n' and ricCrea , then hasNext will return true , and next will return 'o'.

So you can write something like:

 public static String ricCrea(ListIterator<Character> it) { if (it.hasNext()) { Character c = it.next(); if (it.hasNext()) { return c + ricCrea(it); } } return ""; } 
0
source

The hasnext () method just lets you know if a character is missing from the list or not. next () is a method that gets the value of the sign. Thus, hasnext () does not increase the position of the iterator. It simply indicates whether there is any element in the next position or not, based on which you can call next () or perform the required operation.

0
source

Source: https://habr.com/ru/post/1486145/


All Articles