I have an ArrayList on Java to store several orders from a factory. These orders have several attributes, amongst them is idPedido.
Thing is, if I declare orders 1, 2 and 3, delete order 2 via a method called bajaPedido (Shown below):
public void bajaPedido(){
try{
idPedido=teclado.nextInt();
listaPedidos.remove(idPedido);
}catch(InputMismatchException e){
teclado.next();
System.out.print("Debe utilizar números, por favor, repita el proceso");
}catch(IndexOutOfBoundsException e){
teclado.next();
System.out.println("No existe un pedido con ese ID");
}
}
then the 3rd order will be on the second place of the array, because idPedido in this situation refers to the position of the array, not to the variable stored inside the object.
Question is, how could I code it in a way that if I type in the id it will search up which object contains such id and removes it, instead of removing the position the id equals to?