1

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?

3 Answers 3

1

Use Iterator as shown below:

idPedido = teclado.nextInt();
for(Iterator<Order> itr = listaPedidos.iterator(); itr.hasNext();){
    Order order = itr.next();
    if(order.getId() == idPedido){
        itr.remove();
    }
}

If the id of all the orders in listaPedidos are unique, use break; after itr.remove(); for a better performance (i.e. to prevent the loop running even after removing the desired order).

Sign up to request clarification or add additional context in comments.

3 Comments

That's an interesting approach too, thanks to you and Jordi now I have a much better insight on how to work with arrays. Thanks a lot!
You are most welcome. This is the recommended way to delete an item from a List.
Just tried, it did work and now i can rework several methods with it. Seriously, many thanks!
1

I think the easiest approch would be to use a Map instead of a list, where the key should be your "idPedido" and value the "pedido object".

Map<Integer, Pedido> map = new HashMap<>();
map.put(idPedido, Pedido);
map.remove(idPedido);

If you want to still use a List, you should remove "Pedido" objects from your "listaPedidos" with this method from list interface in orther to be not bounded to your list index.

boolean remove(Object o)

In that case you will have to implement "equals" method on your Pedido class.

https://docs.oracle.com/javase/8/docs/api/java/util/List.html#remove-java.lang.Object-

1 Comment

Oh I see, now I understand how maps are used, it makes much more sense than using a list. I'll be probably be working with the second method you proposed, since I lack experience using maps and I'm on a tight schedule. Thanks a lot, mate!
1

You can solve this by using stream api

List<Orders> orders= contain orders object
orders.stream().filter(item -> item.getIdPedido().equals("Your Id")).findFirst()
            .map(p -> {
                orders.remove(p);
                return p;
            }).get();

I hope it may work.

1 Comment

I'm using what Arvind suggested but, nonetheless, many thanks for your response and the hindsight on the stream api, it's a nice tool to have on my belt too!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.