我是 2D 数组的新手,虽然我认为我理解它,但我不确定为什么我会得到这个 Index Out of Bounds Exception。我尝试将地图大小设置为 70x100,但仍然收到相同的错误。似乎认为我正在搜索索引-1156638823,我不明白为什么。任何帮助,将不胜感激!
public int rand(int i) {
return new Random(i).nextInt();
}
public void createMap() {
map = new Grid[7][10];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
map[i][j] = new Grid(false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
false, false);
}
}
map[rand(7)][0] = new Grid(true, false, true, true, false, true, false,
false, false, false, false, false, false, false, false, true);
map[rand(7)][rand(10)] = new Grid(true, false, false, false, false,
false, true, false, false, false, false, false, false, false,
false, false);
}
LOGCAT
05-07 19:29:00.183: E/AndroidRuntime(1532): FATAL EXCEPTION: main
05-07 19:29:00.183: E/AndroidRuntime(1532): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.dungeonmaster/com.evorlor.dungeonmaster.DM}: java.lang.ArrayIndexOutOfBoundsException: length=7; index=-1156638823
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.os.Looper.loop(Looper.java:137)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-07 19:29:00.183: E/AndroidRuntime(1532): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 19:29:00.183: E/AndroidRuntime(1532): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 19:29:00.183: E/AndroidRuntime(1532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-07 19:29:00.183: E/AndroidRuntime(1532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-07 19:29:00.183: E/AndroidRuntime(1532): at dalvik.system.NativeStart.main(Native Method)
05-07 19:29:00.183: E/AndroidRuntime(1532): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=7; index=-1156638823
05-07 19:29:00.183: E/AndroidRuntime(1532): at com.evorlor.dungeonmaster.DM.createMap(DM.java:101)
05-07 19:29:00.183: E/AndroidRuntime(1532): at com.evorlor.dungeonmaster.DM.initialize(DM.java:19)
05-07 19:29:00.183: E/AndroidRuntime(1532): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 19:29:00.183: E/AndroidRuntime(1532): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 19:29:00.183: E/AndroidRuntime(1532): at sofia.internal.events.EventDispatcher$MethodTransformer.invoke(EventDispatcher.java:474)
05-07 19:29:00.183: E/AndroidRuntime(1532): at sofia.internal.events.EventDispatcher.invokeTransformer(EventDispatcher.java:136)
05-07 19:29:00.183: E/AndroidRuntime(1532): at sofia.internal.events.EventDispatcher.dispatch(EventDispatcher.java:109)
05-07 19:29:00.183: E/AndroidRuntime(1532): at sofia.app.internal.ScreenMixin.invokeInitialize(ScreenMixin.java:561)
05-07 19:29:00.183: E/AndroidRuntime(1532): at sofia.app.Screen.onCreate(Screen.java:186)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.Activity.performCreate(Activity.java:4465)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-07 19:29:00.183: E/AndroidRuntime(1532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-07 19:29:00.183: E/AndroidRuntime(1532): ... 11 more