Error while removing Object from Arraylist

Asked

Viewed 203 times

2

When I try to remove a int of arrayList he simply of error.

Code:

ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.rgb(119, 103, 141)); colors.add(Color.rgb(33, 10, 208)); colors.add(Color.rgb(203, 100 ,180));
colors.add(Color.rgb(249, 0 ,193)); colors.add(Color.rgb(74, 169, 94)); colors.add(Color.rgb(25, 236, 70));
colors.add(Color.rgb(235, 213, 92)); colors.add(Color.rgb(242, 206, 9)); colors.add(Color.rgb(68, 243, 220));

int teste = colors.get(3);
colors.remove(teste);

Error:

Process: lordlokon.contarapp, PID: 1840
    java.lang.RuntimeException: Unable to start activity ComponentInfo{lordlokon.contarapp/lordlokon.contarapp.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-458559
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-458559
        at java.util.ArrayList.remove(ArrayList.java:405)
        at lordlokon.contarapp.MainActivity.randomizar(MainActivity.java:63)
        at lordlokon.contarapp.MainActivity.onCreate(MainActivity.java:52)
        at android.app.Activity.performCreate(Activity.java:6245)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5443) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
  • Your Arraylist is of Integer or Color objects? this way will give error.

  • @Ironman when I put ArrayList<Color> he asks to change to int

  • You are trying to remove an item(Indice = 3) that does not exist. The index starts at 0

  • @ramaral I added 9 colors(0 to 8), why there is no index 3 ??

  • I didn’t notice there was more than one colors.add() per line.

1 answer

4


The problem is in that method remove() is overloaded:

The method get() returns the object(Integer in this case) stored in the indicated position.
How this object is converted to int(int teste) the method remove() to be used is one expecting an index.

Notice this line of error:

Caused by: java.lang.Arrayindexoutofboundsexception: length=12; index=-458559

You are trying to remove the index -458559, which is the value that the Arraylist has at position 3.(Color.rgb(249, 0 ,193))

One possible way to solve it will be like this:

Integer teste = colors.get(3);
colors.remove(teste);

or

int teste = colors.get(3);
colors.remove(colors.indexOf(teste));
  • In the variable teste I am not guarding the Object ?? could you give an example using the code I quoted in the question ??

  • I’m still a little confused, could you give me an example ??

  • I got it sorted, thank you.

  • I don’t know if I can explain it better. If Arraylist was the type ArrayList<String> your code would not give error. The compiler would use the method boolean remove(Object o)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.