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

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -