【发布时间】:2021-12-15 23:25:43
【问题描述】:
我正在做这个短选择程序,当我运行它时,我得到了这个异常,我无法弄清楚为什么我有它,(我使用的软件是 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)
【问题讨论】:
标签: java exception eclipse-plugin runtime-error selection-sort