java - Cycle through array -


supposing had array 4 elements, , want cycle through them until condition met, best way have so? idea like:

arraylist<player> mylist = new arraylist<player>(); mylist.add(new player("a")); mylist.add(new player("b")); mylist.add(new player("c"));  for(int = 0; < 3; i++) {      if(isgameover)          break;       if(i == 2)         = 0; } 

but thought perhaps there more elegant solution...

the condition breaks loop should used in while loop:

int = 0; while (!isgameover) {     // here.      = (i + 1) % 3; } 

here, % modulus operator; i.e. remainder when dividing 3. way, i 0, 1, 2, 0, 1, 2, ...


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -