Java 为什么它不停地说;ArrayIndexOutOfBoundsException“;在我的密码上?
因此,我对编码是新手,我正在使用Drjava。Java 为什么它不停地说;ArrayIndexOutOfBoundsException“;在我的密码上?,java,arrays,sorting,Java,Arrays,Sorting,因此,我对编码是新手,我正在使用Drjava。 我刚从我的类中得到一个任务,要求用户将整数输入到两个数组中(用户必须在每个数组中输入从最小到最大的整数)。 然后,我必须将这两个数组合并,并将新数组从最小到最大排序。 我想我知道了怎么做,它会编译得很好,但在我运行代码并输入一些数字后,它一直说有一个OutOfBoundsException 为什么它一直这样做,我该如何修复它 谢谢 class Main{ public static void main (String str[]) th
我刚从我的类中得到一个任务,要求用户将整数输入到两个数组中(用户必须在每个数组中输入从最小到最大的整数)。
然后,我必须将这两个数组合并,并将新数组从最小到最大排序。
我想我知道了怎么做,它会编译得很好,但在我运行代码并输入一些数字后,它一直说有一个OutOfBoundsException 为什么它一直这样做,我该如何修复它 谢谢
class Main{
public static void main (String str[]) throws IOException {
Scanner scan = new Scanner (System.in);
System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
int[] array1= new int[10000];
int x=0;
int x1=0;
int v=1;
for(int where=1; x>=0; where++) {
x= scan.nextInt();
array1[where]=x;
x1++;
if((array1[where]<array1[where-1])&&(x>=0)){
System.out.println("ERROR: Array not in correct order");
x=-326;}
}
System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
int[] array2= new int[10000];
int y=0;
int y1=0;
int w=1;
for(int wher=1; y>=0; wher++) {
y= scan.nextInt();
array2[wher]=y;
y1++;
if((array2[wher]<array2[wher-1])&&(y>=0)){
System.out.println("ERROR: Array not in correct order");
y=-326;}
}
if(x!=-326) {
System.out.println("First Array: ");
int where=x1;
for(v=1; v<(where); v++) {
System.out.println(array1[v]); }}
if(y!=-326) {
System.out.println("Second Array: ");
int wher=y1;
for(w=1; w<(wher); w++) {
System.out.println(array2[w]); }}
int[] array3= new int[v+w];
int a=0;
int b=0;
while((a+b)<(v+w-3)) {
while(array1[v-a]>array2[w-b]) {
array3[w+v-a-b]=array1[v-a];
a++;}
while(array2[w-b]>=array1[v-a]) {
array3[v+w-a-b]=array2[w-b];
b++;}
}
if((y!=-326) && (x!=-326)) {
System.out.println("Merged Array: ");
int c=0;
while((v+w-c)>=2){
System.out.println(array3[v+w-c]);
c++; }
}
}
}
主类{
公共静态void main(字符串str[])引发IOException{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入第一个数组的值,最多10000个值,输入一个负数退出”);
int[]数组1=新int[10000];
int x=0;
int-x1=0;
int v=1;
对于(int其中=1;x>=0;其中++){
x=scan.nextInt();
阵列1[其中]=x;
x1++;
if((数组1[其中]=0)){
System.out.println(“错误:数组顺序不正确”);
x=-326;}
}
System.out.println(“输入第二个数组的值,最多10000个值,输入一个负数退出”);
int[]array2=新int[10000];
int y=0;
int y1=0;
int w=1;
对于(int-wher=1;y>=0;wher++){
y=scan.nextInt();
阵列2[wher]=y;
y1++;
如果((阵列2[wher]=0)){
System.out.println(“错误:数组顺序不正确”);
y=-326;}
}
如果(x!=-326){
System.out.println(“第一个数组:”);
int,其中=x1;
对于(v=1;v=2){
系统输出打印LN(数组3[v+w-c]);
C++;
}
}
}
在第一个for循环中,当where=10000
时,它尝试访问不存在的array1[10000]
。因此,您试图访问一个“超出范围”的索引
您需要确保不尝试访问不存在的索引
由于您定义了
array1=newint[10000]
,这意味着您只能访问索引0-9999。任何超过array1[9999]
的内容都将抛出相同的错误“ArrayIndexOutOfBounds”。我认为这是一个非常明显的错误信息。您应该使用更简单的变量。我第一次看到有人在for循环中使用where作为整数变量:-PFYI,如果您使用相关的语言标记(ie),代码块将突出显示您的语法。您可能会考虑的一件事是将变量命名为逻辑变量。上一次我检查-一个字母不是一个很好的变量名,除非你写一个代数例程…@Matthew Wu FYI…,数组是零索引的。这意味着您的10000个元素的数组实际上使用0到10的索引9999@MatthewWu请接受解决您问题的答案。上面的答案似乎是正确的,至少你可以给他一个公认的答案来解决你的问题。。