Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayIndexOutOfBoundsException-ArrayLength_Java_Arrays_Sorting_Indexoutofboundsexception_Swap - Fatal编程技术网

Java ArrayIndexOutOfBoundsException-ArrayLength

Java ArrayIndexOutOfBoundsException-ArrayLength,java,arrays,sorting,indexoutofboundsexception,swap,Java,Arrays,Sorting,Indexoutofboundsexception,Swap,你好,你能帮我吗?我的代码有什么问题?我知道数组的长度或If语句中有错误,但我无法找到它 多谢各位 import java.util.Scanner; class TripleSwapping { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter the count of your element

你好,你能帮我吗?我的代码有什么问题?我知道数组的长度或If语句中有错误,但我无法找到它

多谢各位

import java.util.Scanner;

 class TripleSwapping {

 public static void main(String[] args) {

     Scanner in = new Scanner(System.in);
     System.out.println("Please enter the count of your elements: ");
     int count = in.nextInt();

     System.out.println("Your array before sorting: ");
     int A[] = new int [count];
     for (int i = 0; i < 10; i++) {
         A[i]=in.nextInt();
    }
     boolean changed =false;
     do {
         for (int i = 0; i < A.length-1; i++) {
             if (!(A[i+1]>A[i]) && !(A[i+1] > A[i+2])) {
                 int temp;
                 temp = A[i];
                 A[i]= A[i+1];
                 A[i+1] =temp;
               }
         }
         for (int i = 0; i < A.length-1; i++) {
             if (A[i] >A[i+1]) {
                  int temp;
                 temp = A[i];
                 A[i]= A[i+1];
                 A[i+1] =temp;
               changed=true;  
             }

         }


     }while(changed);

     System.out.println("Your array after swapping");

     for (int i = 0; i < A.length; i++) {
         System.out.println(A[i]);
     }

}

}
import java.util.Scanner;
类三重映射{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.println(“请输入元素的计数:”);
int count=in.nextInt();
System.out.println(“排序前的数组:”);
整数A[]=新整数[计数];
对于(int i=0;i<10;i++){
A[i]=in.nextInt();
}
布尔值=假;
做{
for(int i=0;iA[i])&(!(A[i+1]>A[i+2])){
内部温度;
温度=A[i];
A[i]=A[i+1];
A[i+1]=温度;
}
}
for(int i=0;iA[i+1]){
内部温度;
温度=A[i];
A[i]=A[i+1];
A[i+1]=温度;
更改=正确;
}
}
}同时(改变);
System.out.println(“交换后的数组”);
for(int i=0;i
您将获得ArrayIndexOutOfBoundsException,因为如果输入值小于10,它将引发异常

for (int i = 0; i < 10; i++) {
    A[i]=in.nextInt();
}
for(int i=0;i<10;i++){
A[i]=in.nextInt();
}
改用

for (int i = 0; i < count; i++) {
    A[i]=in.nextInt();
}
for(int i=0;i
请将您的代码复制并粘贴到您的问题中。请不要链接到课本的图像。这没有帮助。@markspace是的,我忘记添加代码了,现在我添加了,非常感谢您提醒我:)错误没有告诉您哪一行超出了界限吗?我能看到的最容易的罪魁祸首是在分配
count
变量时:
int count=in.nextInt()
。如果读取的int小于10,那么接下来执行的for循环是:
for(int i=0;iWell,还需要异常跟踪——您得到的异常的全文。它应该有行号。另外,请用注释指出哪一行是实际错误,因为我们看不到代码中的行号。最好使用
i
。因为
count,所以出错的可能性较小t
被重新分配到其他值。是的!!您还可以检查
0
value您输入了什么?