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
Post a Comment