1

Arraylist.remove removes first occurrence instead of index, even though I input an int in .remove(int i)? Let’s I have a arraylist “numbers” of random amount of numbers, for this example lets say we have {1,3,5,2,4,6}

ArrayList<Integer> numbers = new ArrayList<Integer>();
aList.add(1);
aList.add(3);
aList.add(5);
aList.add(2);
aList.add(4);
aList.add(6);

I want to remove all odd numbers based on their indices. I have created another Arraylist containing their indices through:

int origSize = numbers.size();
for (int i = 0; i < origSize; i++) {
            if (numbers.get(i) % 2 != 0) {
                numbers.add(numbers.get(i));
                remover.add(i);
            } 

For some reason, when I use the .remove function to remove an element based on its index, it removes the first occurrence. My syntax is right or, I am doing .remove(int i) :

        for (int i = (remover.size() - 1); i>=0; i--) {
            numbers.remove(remover.get(i));
        }

In my example, 5 occurs at index 2. But running .remove(remover.get(i)), which should be .remove(2) removes the first occurrence of 2… Ultimately, I get {3,5,4,6} because it removes the 2,1 and 0 instead of those indices. Why is this happening?

Thanks so much for your help.

sandboxj
  • 1,234
  • 3
  • 21
  • 47
  • I think you want remove(int) signature – ha9u63a7 Oct 12 '16 at 14:22
  • @SergiiGetman 1. OP doesn't use for-each loop. 2. In each loop there are additions to one list and removals from other. – Jaroslaw Pawlak Oct 12 '16 at 14:25
  • remover.get() will return an Integer, not an int. – GhostCat Oct 12 '16 at 14:26
  • So basically I cannot use an `ArrayList remover = new ArrayList()`; to store the indices? I have to find another approach to store the indices? Does anyone have a suggestion for me, because the amount of numbers/indices has to be flexible. – sandboxj Oct 12 '16 at 14:27
  • The "DUP" comes out of the fact that he is inadvertently using the **wrong** remove method. That is why I put an additional comment there. – GhostCat Oct 12 '16 at 14:32

1 Answers1

9

When you have a List<Integer>, if you want to call remove(int index), you need to pass in an int. If you want to call remove(Object o), you need to pass in an Integer. Integer inherits from Object. int does not.

If you find you are calling the wrong one, cast your int to an Integer or vice versa using numbers.remove((int) ...) or numbers.remove((Integer) ...).

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • To clarify, an Integer is not the same as an integer. An Integer is an object. – kwantuM Oct 12 '16 at 14:24
  • 1
    @kwantuM An integer is not the same as an `int`. – Marko Topolnik Oct 12 '16 at 14:24
  • So basically I cannot use an `ArrayList remover = new ArrayList()`; to store the indices? I have to find another approach to store the indices? Does anyone have a suggestion for me, because the amount of numbers/indices has to be flexible. – sandboxj Oct 12 '16 at 14:27
  • @MarkoTopolnik that's exactly what I said. – kwantuM Oct 12 '16 at 14:28
  • 1
    You can change this line: `numbers.remove(remover.get(i));` to `numbers.remove((int) remover.get(i));`. This will make it work. – marstran Oct 12 '16 at 14:29
  • @kwantuM No, you said `Integer` != integer, but that's not very relevant to the subject. For example `long` and `char` are also integers. – Marko Topolnik Oct 12 '16 at 14:29
  • But it seems like even if I load the element of the remover arraylist into an integer variable first, e.g. initiating a `int index = remover.get(i)` and load the index into the .remove `remove(index)` function it does not work? – sandboxj Oct 12 '16 at 14:29
  • @MarkoTopolnik well alright, I should've said `int` then :) – kwantuM Oct 12 '16 at 14:32
  • @marstran thanks your method solved it. But why would defining an the index as a `int`, like in my previous comment, not solve the problem? – sandboxj Oct 12 '16 at 14:33
  • @kwantuM It's not just hair-splitting, there are problems where the issue is exactly the confusion between `int` and general integer (overflows). – Marko Topolnik Oct 12 '16 at 14:33