有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我总是得到BoundsException的数组?

public class sampleCode {

    public static void main( String[] moe ) {
        int[] larry = new int[moe.length];
        for( int i=0; i<moe.length; i++ ) {
            larry[i] = Integer.parseInt( moe[i] );
        }

        int[] curly = null;
        if( larry.length <= 10 ) {
            curly = path1( larry );
        } else 
            curly = path2( larry );

        // PRINT RESULTS
        System.out.println( curly[0]+", "+curly[1] );
    }

    public static int[] path1( int[] foo ) {
        for( int i=0; i<foo.length; i++ ) {
            for(int j=0; j<foo.length-1; j++) {
                if( foo[j] > foo[j+1] ) {
                    int bar = foo[j];
                    foo[j] = foo[j+1];
                    foo[j+1] = bar;
                }
            }
        }
        return foo;
    }

    public static int[] path2( int[] foo ) {
        int[] z = null;
        if( foo.length > 1 ) {
            int bar = foo.length / 2;
            int[] a = new int[bar];
            int[] b = new int[foo.length - bar];
            for( int x=0; x<foo.length; x++ ) {
                if( x<bar ) a[x] = foo[x];
                else b[x-bar] = foo[x];
            }
            z = path2b( path2( a ), path2( b ) );
        } else {
            z = foo;
        }
        return z;
    }

    protected static int[] path2b( int[] foo, int[] bar ) {
        int i=0;
        int j=0;
        int x=0;
        int[] z = new int[foo.length+bar.length];
        while( i<foo.length || j<bar.length ) {
            if( i < foo.length && j == bar.length ) {
                z[x] = foo[i++];
            } else
            if( j < bar.length && i == foo.length ) {
                z[x] = bar[j++];
            } else
            if( foo[i] < bar[j] ) {
                z[x] = bar[j++];
            } else {
                z[x] = foo[i++];
            }
            x++;
        }
        return z;
    }
}

在Eclipse中运行此代码时,我不断得到一个

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at sampleCode.sampleCode.main(sampleCode.java:18)

这似乎是当我试图System.out.println() 我理解这个异常意味着什么,但我不知道我需要修复代码中的哪些内容才能使它正常运行


共 (3) 个答案

  1. # 1 楼答案

    这意味着卷曲的长度是0而不是2。因此,当您尝试访问curly[1]时,您将无法访问。你的问题你期待卷曲。长度>;1而事实并非如此。通过使用断言来验证您认为正确的内容,可以更好地解决此类问题。例如:

        // PRINT RESULTS
        assert curly.length > 1 :  "curly.length < 2";
        System.out.println( curly[0]+", "+curly[1] );
    

    要使其正常工作,您需要将-ea选项传递给JVM。我不使用Eclipse,所以我不确定如何在Eclipse中执行此操作,但在Intellij中,您可以编辑运行配置并将-ea放在VM选项行中

    在验证输入时,您需要开始做的第二件事。出现此问题的原因是,字符串[]moe中的初始输入不是您所期望的

    public static void main( String[] moe ) {
        if (moe.length < 2){
            System.out.println("Usage: java -jar moe.jar # # ... #");
            System.out.println("where # is a integer and has at least two integers separated by a space.");
            System.exit(0);
        }
    

    你可能有其他问题,但根据你给我们的,这是我可以告诉你的

  2. # 2 楼答案

    @Aify抢先找到了答案。为了你的案子

    moe.length = 0;
    

    既然如此,您的初始for循环就永远不会执行,因为i=0不能小于零。自从拉里。length=0同样,您将调用path1,但不会调用循环,因此将调用path1(…)将只返回您的输入

    但是,这就是我为什么要回应的原因。调试器是您的朋友

    看看: http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-stepping.htm

    如果在第6行设置断点并运行调试器,则可以更好地了解问题

    这将使您开始在Eclipse中调试代码

  3. # 3 楼答案

    您的代码正在按预期运行。您将获得ArrayOutOfBounds异常,因为您没有将任何内容传递到Main

    例如,使用参数3和2运行程序时,输出为“2,3”

    仅供参考:您可以使用runconfigurations选项向程序添加参数,以便在Eclipse中使用