0

My friend and I are studying for our programming exam doing the Sample Programming Exam, and are having trouble using an Array List as a parameter in a method to remove multiple items from an Array List in the same class.

We have tried searching the web and using our BlueJ textbook to find a solution.

The instruction on our assignment sheet says, "Write a method removeItemFromOrder with an array of strings itemArray parameter to remove multiple food items from the order."

import java.util.ArrayList;
/**
 * Write a description of class Order here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Order
{
    // instance variables - replace the example below with your own
    private ArrayList<OrderedItem> order;

    /**
     * Constructor for objects of class Order
     */
    public Order()
    {
        order = new ArrayList<>();
    }

    /**
     * Returns the order collection to the user.
     * @return the order collection.
     */
    public ArrayList getOrder()
    {
        return order;
    }

    /**
     * Add an item to the order.
     * @param OrderedItem the item to be added
     */
    public void addOrderItem(OrderedItem foodItem)
    {
        order.add(foodItem);
    }

    public void removeOrderItem(ArrayList<String> itemArray)
    {
        //**We don't know what to put here!**
    }
}
liambl88
  • 1
  • 1
  • Adding array list as parameter https://stackoverflow.com/q/17125270/8631622 removing multiple items from array list https://stackoverflow.com/q/203984/8631622 – Pie Apr 30 '19 at 04:09

4 Answers4

1

if you need to remove multiple items, you can add them to a temporary list and use removeAll too

// creating ArrayList 
java.util.List<String> list = new ArrayList<>();  
list.add("apple");
list.add("banana");
list.add("mango");
list.add("barry");

java.util.List<String> needToRemove = new ArrayList<>();
needToRemove.add("apple");
needToRemove.add("banana");

// removeAll will remove multiple elements,
// We can pass any collection like Set, List or any other
list.removeAll(needToRemove);        

System.out.println("After Removed From List:");
list.forEach(System.out::println);
Madplay
  • 1,027
  • 1
  • 13
  • 25
Ujwala
  • 91
  • 1
  • 2
  • 9
  • check this out https://javadeveloperzone.com/java-basic/java-remove-multiple-objects-from-arraylist/ – Ujwala Apr 30 '19 at 04:25
  • How would we go about this? Would the Array List created in the parameter serve as the temporary list? – liambl88 Apr 30 '19 at 04:25
0

Arraylist.clear(); method can be used to clear the array list Arraylist.removeAll(); method removes all the elements of the array list. Both the methods clear the elements of the list. The clear() method has a better performance due to the less number of lines it has to execute. This link https://howtodoinjava.com/java/collections/arraylist/empty-clear-arraylist/ provides the source codes for the two methods

mehani97
  • 21
  • 5
0

Very good friend, you can try with the method .remove()as long as you have a key of the object, that is secunecial

Fun Code
  • 11
  • 2
  • We are trying to remove items from the main Array List using an Array List as a parameter within the method that removes the items from the main Collection. – liambl88 Apr 30 '19 at 04:24
0

As I see the array list that you pass to removeOrderItem method is a list of strings and your array list in the class is a list of OrderItem. So first you must find what is common between these two lists.

Let's assume the model of OrderItem has a field that contains the name of each order (orderItem.getName()). And the itemArray in the removeOrderItem method is a list that contains names of some orders that you want to remove them.

Ok, in this case, we can do this:

public void removeOrderItem(ArrayList<String> itemArray) {
    List<OrderedItem> tempList = new ArrayList<>();

    for (OrderedItem food: this.order) {
        for (String foodName: itemArray) {
           if (food.getName().equals(foodName))
               tempList.add(food);
               break;
        }
    }

    this.order.removeAll(tempList);
}
SamiAzar
  • 1,260
  • 13
  • 29