asked 202k views
2 votes
Write a variable-arity version of the remove method. The return value is the total number of elements removed from the bag. Your implementation should use the array version of a for-loop from page 111.

asked
User Chus
by
8.2k points

1 Answer

5 votes

Final answer:

To write a variable-arity version of the remove method, you can use the remove All method from the java.util.ArrayList class.

Step-by-step explanation:

To write a variable-arity version of the remove method, you can use the removeAll method from the java.util.ArrayList class. This method takes a collection as its argument and removes all occurrences of elements in that collection from the list, returning the number of elements removed.

Here's an example:

public int remove(Object... elements) {
 List<Object> list = new ArrayList<>(Arrays.asList(elements));
 return myArrayList.removeAll(list);
}

In this example, the remove method accepts variable arguments, converts them to a list, and uses the removeAll method to remove all occurrences of those elements from the myArrayList. The return value is the number of elements removed.

answered
User Viral Shah
by
7.6k points