I am new to 2D Arrays, and while I think I understand it, I am unsure why I am getting this Index Out of Bounds Exception. I tried making the map size 70x100, and I still received the same error. It seems to think I am searching for the index -1156638823, which I do not understand why. Any help would be appreciated!
我是2D阵列的新手,虽然我认为我理解它,但我不确定为什么我会得到这个超出界限的索引。我尝试使地图大小为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
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
4 个解决方案
#1
4
You are misusing Random
. You have invoked the Random
constructor that sets the initial seed to a specific value: i
. But it does not restrict the random values that come back.
你在滥用随机。您已调用Random构造函数将初始种子设置为特定值:i。但它不会限制返回的随机值。
The nextInt()
method returns a random, unrestricted int
.
nextInt()方法返回一个随机的,不受限制的int。
You need the following code:
您需要以下代码:
public int rand(int i) {
return new Random().nextInt(i);
}
The nextInt(int)
method restricts the random integers returned to the range 0
to i - 1
. Also, the parameterless Random
constructor uses the system time to generate a different random seeding (and thus different random numbers) each time.
nextInt(int)方法将返回的随机整数限制为0到i-1。此外,无参数的Random构造函数使用系统时间每次生成不同的随机种子(因而不同的随机数)。
Random numbers in Java using Random
are pseudo-random - they are deterministic but they require a seed to get started. Using the same seed will get you the same random number back. That's why it's important to use different seeds each time, and the parameterless Random
constructor handles that for you.
使用Random的Java中的随机数是伪随机的 - 它们是确定性的,但它们需要种子才能开始。使用相同的种子将获得相同的随机数。这就是为什么每次使用不同的种子很重要,无参数的Random构造函数会为你处理。
In addition, you don't need to be creating another Random
object each time your rand
object is called. You could create it as an instance variable in your class and just refer to it in your rand
method.
此外,每次调用rand对象时都不需要创建另一个Random对象。您可以在类中将其创建为实例变量,并在rand方法中引用它。
#2
4
new Random(i).nextInt()
doesn't do what you think it does.
new Random(i).nextInt()不会按照您的想法执行。
You should use:
你应该使用:
new Random(seed).nextInt(i)
Your version just initialises the seed but nextInt()
return an integer in the whole range, if you want a smaller range you need to specify it, by specifying nextInt(i)
you will get a value in [0,1)
您的版本只是初始化种子但nextInt()返回整个范围内的整数,如果您需要指定它的较小范围,通过指定nextInt(i)您将获得[0,1]中的值
#3
0
You used the wrong method: Random#nextInt()
. You thought you used Random#nextInt(bound)
instead, which yields the random integer within the range 0
(incl.) - bound
(excl.). The integer in Random
constructor is just a random seed.
您使用了错误的方法:Random#nextInt()。你以为你使用了Random#nextInt(bound),它产生的范围是0(incl。) - 范围内的随机整数(不包括在内)。 Random构造函数中的整数只是一个随机种子。
#4
0
You are using the Random
constructor wrong. The parameter in it is a seed. Not a maximum number it will generate.
您正在使用Random构造函数错误。其中的参数是种子。它不会产生最大数量。
You can read about what a seed is for example here: http://en.wikipedia.org/wiki/Random_seed
您可以在这里阅读有关种子的内容:http://en.wikipedia.org/wiki/Random_seed
If you need it to generate numbers from 0 to X, you need to provide the X to the nextInt
method.
如果需要它来生成从0到X的数字,则需要将X提供给nextInt方法。
new Random().nextInt(X);
#1
4
You are misusing Random
. You have invoked the Random
constructor that sets the initial seed to a specific value: i
. But it does not restrict the random values that come back.
你在滥用随机。您已调用Random构造函数将初始种子设置为特定值:i。但它不会限制返回的随机值。
The nextInt()
method returns a random, unrestricted int
.
nextInt()方法返回一个随机的,不受限制的int。
You need the following code:
您需要以下代码:
public int rand(int i) {
return new Random().nextInt(i);
}
The nextInt(int)
method restricts the random integers returned to the range 0
to i - 1
. Also, the parameterless Random
constructor uses the system time to generate a different random seeding (and thus different random numbers) each time.
nextInt(int)方法将返回的随机整数限制为0到i-1。此外,无参数的Random构造函数使用系统时间每次生成不同的随机种子(因而不同的随机数)。
Random numbers in Java using Random
are pseudo-random - they are deterministic but they require a seed to get started. Using the same seed will get you the same random number back. That's why it's important to use different seeds each time, and the parameterless Random
constructor handles that for you.
使用Random的Java中的随机数是伪随机的 - 它们是确定性的,但它们需要种子才能开始。使用相同的种子将获得相同的随机数。这就是为什么每次使用不同的种子很重要,无参数的Random构造函数会为你处理。
In addition, you don't need to be creating another Random
object each time your rand
object is called. You could create it as an instance variable in your class and just refer to it in your rand
method.
此外,每次调用rand对象时都不需要创建另一个Random对象。您可以在类中将其创建为实例变量,并在rand方法中引用它。
#2
4
new Random(i).nextInt()
doesn't do what you think it does.
new Random(i).nextInt()不会按照您的想法执行。
You should use:
你应该使用:
new Random(seed).nextInt(i)
Your version just initialises the seed but nextInt()
return an integer in the whole range, if you want a smaller range you need to specify it, by specifying nextInt(i)
you will get a value in [0,1)
您的版本只是初始化种子但nextInt()返回整个范围内的整数,如果您需要指定它的较小范围,通过指定nextInt(i)您将获得[0,1]中的值
#3
0
You used the wrong method: Random#nextInt()
. You thought you used Random#nextInt(bound)
instead, which yields the random integer within the range 0
(incl.) - bound
(excl.). The integer in Random
constructor is just a random seed.
您使用了错误的方法:Random#nextInt()。你以为你使用了Random#nextInt(bound),它产生的范围是0(incl。) - 范围内的随机整数(不包括在内)。 Random构造函数中的整数只是一个随机种子。
#4
0
You are using the Random
constructor wrong. The parameter in it is a seed. Not a maximum number it will generate.
您正在使用Random构造函数错误。其中的参数是种子。它不会产生最大数量。
You can read about what a seed is for example here: http://en.wikipedia.org/wiki/Random_seed
您可以在这里阅读有关种子的内容:http://en.wikipedia.org/wiki/Random_seed
If you need it to generate numbers from 0 to X, you need to provide the X to the nextInt
method.
如果需要它来生成从0到X的数字,则需要将X提供给nextInt方法。
new Random().nextInt(X);