-3

I want to remove the array int[]{1,2,3,4] from the list.

We can remove it from the list by the code list.remove(2) (2 is the index). But how we can remove this array by the function remove(Object obj) I thank you so much.

List list = new ArrayList();
    list.add("hello");
    list.add(10);
    list.add(new int[] { 1, 2, 3, 4 });

1 Answers1

1

So, What's the issue here? Just cast the Int to an object.

list.remove((Object) 10);

Since you said you want to remove the int[] which wasn't specified in the question you can do so like this

Okay so I have put together a method.

public void removeIntArray(List list, int[] toRemove) {
    ListIterator listIterator = list.listIterator();
    while (listIterator.hasNext()) {
        Object object = listIterator.next();
        if (object instanceof int[]) {
            int[] ints = (int[]) object;
            if (Arrays.equals(ints, toRemove)) {
                listIterator.remove();
                break;
            }
        }
    }
}

Just call it as

removeIntArray(list, new int[]{1, 2, 3, 4});
SamHoque
  • 2,978
  • 2
  • 13
  • 43