How to search an arraylist with iterator and modify an object

0

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)

    
asked by Juanfe Di Leo 22.05.2018 в 21:20
source

2 answers

1

You make two calls to i.next (), for example, say the arraylist has 2 objects, Nave1 and Nave 2:

@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){
        //Aquí se llama por primera vez a i.next, por tanto aqui es nave1
        if(i.next().equals(nave)){
            //Aqui se llama por segunda vez a i.next, es decir, aqui es nave2
            Nave cambiar = (Nave) i.next();
            cambiar.posicion.incrementarPos(x,y);
            z=1;
        }
        i.next();
    }    
}
}

Therefore, in the loop, just make a single call to i.next () and store it in a variable.

@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){
        Nave naveNueva = i.next();
        if(naveNueva.equals(nave)){

            naveNueva.posicion.incrementarPos(x,y);
            z=1;
        }
    }    
}
}
    
answered by 22.05.2018 / 21:44
source
1

You need to put what type of exception throws you, but I'm seeing the following errors:

  @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)){ //aquí estas avanzando el iterador
            Nave cambiar = (Nave) i.next(); //aquí lo estas volviendo a avanzar. La nave que te de, no va a ser la del equals
            cambiar.posicion.incrementarPos(x,y);
            z=1;
        }
        i.next(); //aquí lo estás volviendo avanzar.
    }    
}

}

Try this way:

 @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){
           Nave naveIterada = i.next();
           if(naveItearda != null && naveIterada.equals(nave)){
              naveIterada.posicion.incrementarPos(x,y);
              z=1;
           }
           /// i.next(); <<este ya no lo requieres, ya iteraste 
        }    
    }
 }

On the other hand, the public methods you have are not validating that the Ship parameter is null. I would add in those the validation:

  public void agregaNave(Nave nave) {
      if (nave == null) {throw new IllegalArgumentException()");
      //etc...
  }

That would put it to avoid that there is no nullity in the list of ships.

Greetings.

    
answered by 22.05.2018 в 21:48