I'm having a bit of trouble creating a simple array in Java where a random number is generated every time it loops. My code is as follows:
public class Q1 {
public static void main(String[] args) {
Scanner listScan = new Scanner(System.in);
System.out.println("Size of list to sort?");
int j = listScan.nextInt();
int listArray[] = new int[j];
for (int i = 0; i <= j; i++){
listArray[i] = (int)(Math.random() * 100 +1);
}
System.out.println(listArray);
}
}
but the code gives me this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Lab3.Q1.main(Q1.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
int listArray[] = new int[j]; // Assume j =5. creates array of 5 elements.
for (int i = 0; i <= j; i++){ // iterates from index 0 to 5 i.e, 6 elements . change it to i<j. it will work.
listArray[i] = (int)(Math.random() * 100 +1);
}
Note : You get ArrayIndexOutOfBoundsException
when you try to access an index which is greater than the array's length.
You're 1 index too high. Use
for (int i = 0; i < j; i++)
Take a look at this part of your code:
int listArray[] = new int[j];
for (int i = 0; i <= j; i++){
listArray[i] = (int)(Math.random() * 100 +1);
}
It should be changed to this:
int listArray[] = new int[j];
for (int i = 0; i < j; i++){
listArray[i] = (int)(Math.random() * 100 +1);
}
Notice how <=
was changed to <
:
for (int i = 0; i <= j; i++){
vs:
for (int i = 0; i < j; i++){
For pretty much all coding, including java
, counting starts at 0
. So where you would normally say 5
, in coding you would say 4
.
Let's say the length of the array is 3
, meaning it contains 0, 1, and 2
. So, when doing this:
for (int i = 0; i <= j; i++){
j
= the length of the array, so 3.
for (int i = 0; i <= 3; i++){
now, it will loop threw the numbers 0, 1, 2, and 3
. Yet, our array does not contain a 3, it contains only 0, 1, and 2
. So, if we were to do this:
for (int i = 0; i < j; i++){
j
= 3
for (int i = 0; i < 3; i++){
Using this new code, it will loop threw 0, 1, and 2
, the same size of our array, 0, 1, and 2
.
Two problems:
You're for-loop is going 1 index position too far. You can't include the actual j
value as an index position. The last index position is 1 less than the length. So do this:
for (int i = 0; i < j; i++) {
You're not printing your array properly. You can't print an array like this: System.out.println(array);
so you have two options:
System.out.println( Arrays.toString(listArray) ); // or iterate through the array for (int i = 0; i < listArray.length; i++) { System.out.println(listArray[i]); }
The problem is, the array, in Java is an static structure, and it won´t change it size directly, as you add elements. You can use another types of objects, more suitable as ArrayList.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.