1

I am trying to delete an item from an array (obv, if the array contains it) but it is always giving me an IndexOutOfBoundsException while trying to remove it.

I don't think this question is the same as the one pointed out in the comment section, beacause I was trying to figure out why is this giving me an error in this specific scenario.

I've been searching any solution that could possibly solve it but I found nothing, so I hope someone here is able to tell my why is this happening.

The array I am trying to edit is an Integer array (converted from an String array). Let me show you some code.

for (final Alarm alarm : allAlarms) {
    ArrayList<Integer> activos = IntegerArrayConverter.fromString(alarm.getActiveTags());
    if (activos.contains(idTag)) {
        activos.remove(idTag);
        alarm.setActiveTags(IntegerArrayConverter.fromArrayList(activos));
        app.updateAlarmActiveTags(alarm);
    }
}

It is crashing where it says:

active.remove(idTag);

When the app reaches that line, my array is size 1 and its only value is 255. The idTag value is also 255. I don't know where is the error.

Thank you in advance!

activos arrat content

idTag value

SOLUTION:

As they told me in the comments section, I moved the creation of the ArrayList outside the for loop and also used:

activos.remove(Integer.valueOf(idTag));

instead of:

activos.remove(idTag);
P. Vera
  • 315
  • 4
  • 11
  • 4
    move your arraylist creation outside your for loop otherwise it will create arraylist everytime your loop runs time. – karan Feb 11 '19 at 08:56
  • 2
    Also, the method might think you are referring to arrayList.remove(index i) and not arrayList.remove(object o). Try 'active.remove(Integer.valueOf(idTag))' or 'active.remove(new Integer(idTag))' – Zee Feb 11 '19 at 09:01
  • @GhostCat That's okay for me. But the linked answer only explain why the remove method is behaving incorrectly when passing an Integer as argument. – LppEdd Feb 11 '19 at 09:05
  • 1
    Thenk you for your comments, I am trying some of the solutions and will let you know as soon as I get to what it works for me. – P. Vera Feb 11 '19 at 09:23
  • And btw, I don't really see how my question would be the same as the one pointed out by @GhostCat, actually. – P. Vera Feb 11 '19 at 09:24
  • As i said, he wants to know how and why the above code results in a ArrayIndexOutOfBoundsException and not how to remove something properly. – Alan Feb 11 '19 at 09:26
  • let me refer to his post: "so I hope someone here is able to tell my why is this happening." – Alan Feb 11 '19 at 09:26
  • Thank you @Alan you are right, even though I didn't see the other question before hand and it is really helpful, I also got solutions as the one given by Karan Mer (the first one) which are not given in the one GhostCat talked about. – P. Vera Feb 11 '19 at 09:31
  • Thank you all again, I am now creating my ArrayList outside the loop and using activos.remove(Integer.valueOf(idTag)) and now my code is working. – P. Vera Feb 11 '19 at 09:35

3 Answers3

1

The ArrayList class has 2 overloads method to remove. One with an integer parameter, that removes the item at that index, and one with an Object parameter.Overload resolution in Java always starts without consideration of boxing and unboxing. Therefore it gives priority to the remove(int) overload. Should pass index to the remove method of Arraylist. trying replacing line with

activos.remove(0); or activos.remove(new Integer(255)); It is not recommended to use ArrayList.remove() when iterating over elements

  • @Alan i was trying to execute sample code in eclipse using system. – Avvappa Hegadyal Feb 11 '19 at 09:14
  • Thank you so much, you can see in the comments what I used to make it work! I guess **Integer.valueOf()** is similar as **new Integer()**, right? – P. Vera Feb 11 '19 at 09:37
  • @P.Vera dont ask people here, consider putting such questions into google first: that is the point: almost **anything** you can dream of asking here ... was asked here before. and answered. https://stackoverflow.com/questions/9030817/differences-between-new-integer123-integer-valueof123-and-just-123 – GhostCat Feb 11 '19 at 09:42
  • @GhostCat I was making sure before accepting his answer. I will remember for further ocations anyway. – P. Vera Feb 11 '19 at 09:45
1

ArrayList class has 2 methods

first is: list.remove(index):- its remove index value

second is : list.remove(object): - its remove particular object

In interger type arraylist to remove particular object from arraylist

Java Code

use this code

 if(list.contains(255)){

        list.remove(Integer.valueOf(255));
    }
Android Geek
  • 8,956
  • 2
  • 21
  • 35
1

Avvappa Hegadyal mentioned correctlly that remove can be by index (type int) or by object (some non primitive type).

You have encountered IndexOutOfBoundException, because the first overload method has been called (removing element by id) - but In your array there is no index 255 (you have only one element with index 0)

If you want to remove the element 255. Than make sure that the type of the variable idTag is Integer (instead of int). So the second overloaded method will be called.