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.
– Iron Man
@Ironman when I put
ArrayList<Color>
he asks to change toint
– Lucas Caresia
You are trying to remove an item(Indice = 3) that does not exist. The index starts at
0
– ramaral
@ramaral I added 9 colors(0 to 8), why there is no index 3 ??
– Lucas Caresia
I didn’t notice there was more than one
colors.add()
per line.– ramaral