【问题标题】:Iam having a runtime error while running my selection short program in java [duplicate]在java中运行我的选择排序程序时出现运行时错误[重复]
【发布时间】: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


    【解决方案1】:

    更改下面的行并尝试。

    a[minnd]=a[temp];

    a[minnd]=温度;

    如果这解决了 ArrayIndexOutofBoundException,请告诉我。

    【讨论】:

    • Thnx 它解决了异常
    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多