首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >命令行参数在“主”线程中导致异常。

命令行参数在“主”线程中导致异常。
EN

Stack Overflow用户
提问于 2015-02-03 18:28:24
回答 2查看 140关注 0票数 0
代码语言:javascript
复制
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");
        }
    }

因此,当我编译这个文件(这不是我的全部文件)时,它会编译,只是当我试图运行它时,它就会出现异常:

代码语言:javascript
复制
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at bubblesort.ascending(bubblesort.java:61)
    at bubblesort.main(bubblesort.java:23)

我认为问题在于数组长度,或者我称之为“输入”,并没有改变为命令行参数整数,所以输入保持在零。如果是这样,我怎样才能解决这个问题呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-03 18:33:41

在解析用户输入之前,不能初始化arr。语句public int[] arr= new int[input];的结果是一个大小为0的数组,因此任何访问它的记录都会引发异常。在解析数组长度之后,尝试初始化主数组中的数组。

票数 2
EN

Stack Overflow用户

发布于 2015-02-03 18:33:39

在你有变量的顶端。

代码语言:javascript
复制
public int input; //NOT INITIALIZED
public int[] arr= new int[input];

在尝试创建数组之前,没有初始化输入。所以它没有尺寸。

尝试使用主函数中的用户输入初始化它,然后创建数组。

代码语言:javascript
复制
int arrayInput= in.nextInt();
arr = new int[arrayInput];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28306149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档