运行 我在 java 中选择排序程序时出现运行时错误
Iam having a runtime error while running my selection short program in java
我正在做这个选择短的做空程序,当我 运行 它时我得到了这个异常,我无法弄清楚为什么我有它,(我使用的软件是 Eclipse)
这是代码
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of matrix");
int len=sc.nextInt();
int a[]=new int[len];
System.out.println("Enter the series");
for(int k=0; k<len; k++) {
a[k] = sc.nextInt();
}
for(int i=0;i<len-1;i++) {
int minind=i;
for(int j=0; j<len-1-i; j++) {
if(a[j] < a[minind]) {
minind=j;
}
int temp=a[i];
a[i]=a[minind];
a[minind]=a[temp];
}
}
System.out.println("Shorted array is ");
for(int item : a) {
System.out.print(item + " ");
}
这是我的意见
输入矩阵的长度
4
进入系列
4 5 6 1
这里是例外
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at arrays.Selection_short.main(Selection_short.java:27)
更改下面的行并尝试。
a[minind]=a[temp];
至
a[minind]=temp;
让我知道这是否解决了 ArrayIndexOutofBoundException。
我正在做这个选择短的做空程序,当我 运行 它时我得到了这个异常,我无法弄清楚为什么我有它,(我使用的软件是 Eclipse) 这是代码
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of matrix");
int len=sc.nextInt();
int a[]=new int[len];
System.out.println("Enter the series");
for(int k=0; k<len; k++) {
a[k] = sc.nextInt();
}
for(int i=0;i<len-1;i++) {
int minind=i;
for(int j=0; j<len-1-i; j++) {
if(a[j] < a[minind]) {
minind=j;
}
int temp=a[i];
a[i]=a[minind];
a[minind]=a[temp];
}
}
System.out.println("Shorted array is ");
for(int item : a) {
System.out.print(item + " ");
}
这是我的意见
输入矩阵的长度
4
进入系列
4 5 6 1
这里是例外
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at arrays.Selection_short.main(Selection_short.java:27)
更改下面的行并尝试。
a[minind]=a[temp];
至
a[minind]=temp;
让我知道这是否解决了 ArrayIndexOutofBoundException。