java - Stop an iterator at an index -


i trying stop iterator @ index.

example, start off abcdef , if type iterator(3), want return abc , not rest. below have tried doing.

if(stop>this.size() ){     throw new indexoutofboundsexception();  } iterator<e> iterator = iterator(); int  = 0 ;   while (iterator.hasnext()) {     if(i<stop){         iterator.next();         i++;     }     else{         break;     } } 

this way it. because think op suggest stop end of loop.

if(stop>this.size() ){ throw new indexoutofboundsexception(); } else { iterator<e> iterator = iterator();  (int j = 0; j <stop ; j++) {     if(iterator.hasnext()){      // todo      }else {          break;      }   } } 

Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -