Error removing object from an ArrayList

2

When I try to remove a int from arrayList it is simply an 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) 
    
asked by anonymous 06.08.2016 / 20:57

1 answer

4

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

The get () method returns the Integer in this case) saved in the specified position. Since this object is converted to int ( int teste ) the remove() method to be used is the one that expects an index.

Notice this line of error:

  

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

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

One possible way to resolve this will be as follows:

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

or

int teste = colors.get(3);
colors.remove(colors.indexOf(teste));
    
06.08.2016 / 22:16