Loading...
Logo
Processing Forum
Hi all,

I don't know why this int val = it.nextIndex() is not working, it says "The function nextIndex does not exist." I don't have idea why. Can you help me? 

void draw() {
  background(50);
  Iterator<Level> it = myLevel.iterator();
  while (it.hasNext ()) {
    int val = it.nextIndex();
    Level oneLevel = it.next();
    println(val);
    //oneLevel.display(randomNumbers[it.nextIndex()-1]);
  }
}

Replies(5)

According to this official link -> http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html
There are only 3 methods to interface Iterator -> hasNext(), next() and remove().
And nextIndex() is not 1 of them!

Also, I have my doubts whether an int variable would be valid to store its returned value.
Searching further, if by chance, nextIndex() is a method from class Level; you have to use next() 1st:

int val = it .next() .nextIndex();

However if you want to keep using methods from that same instance, you can't use next() again; lest you end up addressing next instance prematurely!

So you would need to store a copy of that instance for those cases!
That's because you want a ListIterator:
Copy code
  1. ArrayList<Level> myLevel = new ArrayList<Level>();
  2. class Level {}
  3.  
  4. void setup() { myLevel.add(new Level()); }
  5.  
  6. void draw() {
  7.   background(50);
  8.   ListIterator<Level> it = myLevel.listIterator();
  9.   while (it.hasNext()) {
  10.     int val = it.nextIndex();
  11.     Level oneLevel = it.next();
  12.     println(val);
  13.     //oneLevel.display(randomNumbers[it.nextIndex()-1]);
  14.   }
  15. }

Oops! Still newbie @ Java!
Forgot there are more specialized & powerful classes and interfaces like ListIterator -> http://docs.oracle.com/javase/6/docs/api/java/util/ListIterator.html

Never used them anyways! 
Hey PhiLho 

Thanx for your answer, that is exactly what I was searching for. 
all the best

David