我想要做的是用随机数填充20个整数的数组,然后打印出每个整数重复自身的次数。我越来越想打印出的重复次数时的错误...这里是我的代码:打印出一个数字在一个数组中重复的次数(ArrayIndexOutofBoundException)
package nivel3;
import java.util.Random;
public class exercicio3 {
public static void main(String[] args) {
int[] numeros;
int i=0;
numeros=new int[20];
Random rand=new Random();
System.out.println("FILLING ARRAY WITH 20 RANDOM INTEGERS (FROM 0-9) \n");
do {
for (i=0; i<20; i++) {
numeros[i]=rand.nextInt(10);
System.out.printf("position"+i+of numeros[20]: "+numeros[i]+"\n\n");
}
} while (i<20);
System.out.println("--------------------------------------------------------------------- \n");
System.out.println("SHOWING THE REPEATED POSITIONS (FOR EACH POSITION OF THE ARRAY)\n");
for(int j=0; j<20; j++) {
int k=0;
do {
k++;
if (numeros[j]==numeros[k])
System.out.printf("the integer in the ["+j+"] position is equal to the integer in the ["+k+"] position \n");
}while (k<20);
}
}
}
这里的错误代码:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at nivel3.exercicio3.main(exercicio3.java:29)
'新的随机()。整型().limit(20).boxed()。collect(Collectors.toMap(Function.identity(),1,Integer :: sum))'。完成。 –
你明白那个错误是什么意思吗?你看过吗?这是一个非常常见的错误,也是在大多数情况下最容易解决的错误之一。 – Carcigenicate