像这样使用 ArrayList 集合类的 get 和 set 出现问题的任何原因,我都会遇到运行时错误
想要将 Prize 对象放在一个ArrayList
“ oneList ”中的 y 位置到另一个ArrayList
名为“ rungame ”的 y 位置
ArrayList<Prize> xrt = new ArrayList<Prize>();
ArrayList<Prize> rungame = new ArrayList<Prize>();
for(int y = 0; y < xrt.size(); y++){
rungame.set(y, xrt.get(y));
}
日志输出:
01-29 16:16:42.450: E/AndroidRuntime(2644): FATAL EXCEPTION: main
01-29 16:16:42.450: E/AndroidRuntime(2644): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-29 16:16:42.450: E/AndroidRuntime(2644): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-29 16:16:42.450: E/AndroidRuntime(2644): at java.util.ArrayList.set(ArrayList.java:477)
01-29 16:16:42.450: E/AndroidRuntime(2644): at lottery.MainLottery.onClick(MainLottery.java:231)
01-29 16:16:42.450: E/AndroidRuntime(2644): at android.view.View.performClick(View.java:3511)
01-29 16:16:42.450: E/AndroidRuntime(2644): at android.view.View$PerformClick.run(View.java:14110)
01-29 16:16:42.450: E/AndroidRuntime(2644): at android.os.Handler.handleCallback(Handler.java:605)
01-29 16:16:42.450: E/AndroidRuntime(2644): at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 16:16:42.450: E/AndroidRuntime(2644): at android.os.Looper.loop(Looper.java:137)
01-29 16:16:42.450: E/AndroidRuntime(2644): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-29 16:16:42.450: E/AndroidRuntime(2644): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 16:16:42.450: E/AndroidRuntime(2644): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 16:16:42.450: E/AndroidRuntime(2644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-29 16:16:42.450: E/AndroidRuntime(2644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-29 16:16:42.450: E/AndroidRuntime(2644): at dalvik.system.NativeStart.main(Native Method)