【问题标题】:Command line arguments is causing Exception in "main" thread命令行参数导致“主”线程中的异常
【发布时间】:2015-04-03 01:49:28
【问题描述】:
import java.util.*;

public class bubblesort {
        public int input;
        public int c;
        public int d; 
        public int swap;
        public int[] arr= new int[input];
        Random rand = new Random();
        Scanner in = new Scanner(System.in);

    public static void main(String[] args) { 
        bubblesort b = new bubblesort();
        Scanner in = new Scanner(System.in);
        System.out.println("Ascending(1),Descending(2),Random(3)");
        int arrayInput= in.nextInt();

        if (arrayInput == 1) {
            b.ascending(args);
            }
        else if (arrayInput == 2) {
            b.descending(args);
            }   
        else if (arrayInput == 3) {
            b.random(args);
            }           
    }


    public void ascending(String[] args){   
        this.input = Integer.parseInt(args[0]);

        for (c = 0; c < input; c++){ 
          arr[c] = rand.nextInt(Integer.MAX_VALUE);
          }

        for (c = 0; c < ( input - 1 ); c++) {
            for (d = 0; d < input - c - 1; d++) {
             if (arr[d] > arr[d+1]){
                  swap       = arr[d];
                  arr[d]   = arr[d+1];
                  arr[d+1] = swap;
                }
            }
        } 
        for (c = 0; c < input; c++) 
        System.out.println(arr[c]);

        long startTime = System.nanoTime();
        for (c = 0; c < input - 1; c++){
            for (d =0; d < input - c -1 ; d++){
                if (arr[d] > arr[d+1]){
                     swap = arr[d];
                     arr[d] = arr[d+1];
                     arr[d+1] = swap;
                     }
                }
            }
        long endTime = System.nanoTime();
        long estimatedTime = endTime - startTime;
        System.out.println("Sorted list of numbers");
        for (c = 0; c < input; c++) 
        System.out.println(arr[c]);
        System.out.println("The time it takes to sort an descending array into an ascending array is "+estimatedTime+" nano seconds");
        }
    }

所以当我编译这个文件(这不是我的整个文件)它编译时,它只是当我尝试运行它时它给出了异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at bubblesort.ascending(bubblesort.java:61)
    at bubblesort.main(bubblesort.java:23)

我认为问题在于数组长度,或者我称之为“输入”没有更改为命令行参数整数,因此输入保持为零。如果是这样,我该如何解决这个问题?

【问题讨论】:

  • 你没有给input分配任何值,所以它是0,你创建了长度为0的数组arr。如果你想使用arr,你必须分配一个更大的值比你的变量 cd
  • 记得使用 IDE 的断点和调试功能。在寻找 ArrayIndexOutOfBoundsException 时,能够进入您的代码并查看数组的长度是非常宝贵的。

标签: java arrays command-line-arguments bubble-sort


【解决方案1】:

在您拥有变量的顶部。

public int input; //NOT INITIALIZED
public int[] arr= new int[input];

在您尝试创建数组之前,您的输入未初始化。所以它没有大小。

尝试在主函数中使用用户输入对其进行初始化,然后创建数组。

int arrayInput= in.nextInt();
arr = new int[arrayInput];

【讨论】:

    【解决方案2】:

    您无法在解析用户输入之前初始化arr。语句public int[] arr= new int[input]; 产生一个大小为 0 的数组,因此任何访问它的尝试都会引发异常。解析数组长度后,尝试在 main 中初始化数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多