The doubt I have is that I create a list of objects and then I want to use the move method to modify a ship that is in the arraylist and it throws me an error in the if it compares inside the while
public class FlotaEstelar implements InterFlota,Cloneable{
private ArrayList<Nave> lista;
private String nombre;
public FlotaEstelar(String nombre) {
this.nombre = nombre;
this.lista= new ArrayList<Nave>();
}
@Override
public void agregaNave(Nave nave) {
this.lista.add(nave);
}
@Override
public void eliminaNave(Nave nave) {
this.lista.remove(nave);
}
@Override
public Iterator<Nave> iterator() {
return this.lista.iterator();
}
@Override
public Object clone() {
// TODO Implement this method
return null;
}
@Override
public void mover(Nave nave, int x, int y) {
int z=0;
if (this.lista.contains(nave)){
Iterator i=this.lista.iterator();
while(i.hasNext() && z==0){
if(i.next().equals(nave)){
Nave cambiar = (Nave) i.next();
cambiar.posicion.incrementarPos(x,y);
z=1;
}
i.next();
}
}
}
Error:
Exception in thread "main" java.util.NoSuchElementException at java.util.ArrayList $ Itr.next (ArrayList.java:854) at client.FlotaEstelar.mover (FlotaEstelar.java:64) at client.test.main (test.java:17)