淋风沐雨 2021-02-04 13:05 采纳率: 0%
浏览 28

java选择排序:无法调用swapArray方法

package 排序;

public class xuanzhe {
    public static void main(String[] args) {
//        选择排序
        int a[]= {123,1231,21,31,23121,1123};
        System.out.println("请输入数组");
        printArray(a);
        System.out.println("排序完成");
        selectSorr(a);
        printArray(a);
    }

    public static void selectSorr(int[] a) {
        // TODO 自动生成的方法存根
        int i=0;
        int j=0;
        int min_index=0;
        for ( i = 0; i < a.length; i++) {
            min_index=i;
            for (j = i+1; j < a.length; j++) {
                min_index=j;
            }
        }
        if (i!=min_index) {
            swapArray(i,j,a);
        }
    }
//    无法调用swapArray方法
    public static void swapArray(int i,int j, int []a) {
        // TODO 自动生成的方法存根
        int temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }

    public static void printArray(int[] array) {
        // TODO 自动生成的方法存根
        for(int i:array) {
            System.out.print(i+" ");
        }
        System.out.println();
    }
}
 

  • 写回答

3条回答 默认 最新

  • 血舞齐天 2021-02-04 13:24
    关注

    int i与j声明在for循环之外。在最后一次循环中即便没有进入循环体,但是i++执行了,最终i=6数组越界

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法