Iterator

Pessoal tenho a seguinte situação:

public Collection teste(Collection collection)
{
Collection c = new ArrayList();

  Iterator it = collection.iterator();

  while(it.hasNext())
  {
       c.add((metodoQualquer)it.next());
 }

}

Até ai beleza. Mas se eu quiser, ao inves de fazer o iterator correr todo o collection, correr apenas parte dele, tipo da metade para o final, como seria feito? Se não for possivel com iterator, como e com o que seria feito?

Obrigado

Faz isso :

 ao invés de :
 Collection c = new ArrayList(); 
 Usa :
 List c = new ArrayList(); // A Interface List tem como acessar via indice 
  
 E ao invés de um iterator usa um for da forma que achar melhor

Você vai trabalhar especificamente com listas? Pq usar um tipo mais abstrato que uma List?

[code]
/* fiz esse método para “fatiar” uma lista
*cria versões sobrecarregadas do mesmo,
*para fornecer apenas o início ou apenas o fim
*/
public List< T > teste( List< T > lista, int inicio, int fim )
throws IndexOutOfBoundsException {

List< T > retorno = new ArrayList< T >();
int t = lista.size();

if ( inicio < 0 || inicio >= t )
    throw new IndexOutOfBoundsException();
if ( fim < 0 || fim >= t )
    throw new IndexOutOfBoundsException();
if ( fim < inicio )
    throw new IndexOutOfBoundsException();

for ( int i = inicio; i <= fim; i++ )
    retorno.add( lista.get( i ) );

return retorno;

}[/code]

Até mais!