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!
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);