简体   繁体   English

Java编译中无关的内容

[英]Irrelevant content in java compilation

Can someone be so kind to let me know why the heck javac is printing extra lines with zeros when executed the following lines of code? 有人可以这么让我知道为什么在执行以下代码行时,为什么javac会打印多余的零行吗? I have intentionally printed the number of possible lines in the first line, which is exactly equal to the number of nonzero lines. 我特意在第一行中打印了可能的行数,该数量正好等于非零行的数量。 Any advice? 有什么建议吗? Code: 码:

int[] fa = new int[15];
    int[][] sa = new int[100][3];
    int a=0, b=0;

    for(int i=0; i<15; i++){
        fa[i] = i+1;
    }


    for(int i=0; i<fa.length; i++){
        for(int j=i+1; j<fa.length; j++){
            for(int k=j+1; k<fa.length; k++){
                if(fa[i]+fa[j]+fa[k]==15){
                    sa[a][0] = fa[i];
                    sa[a][1] = fa[j];
                    sa[a][2] = fa[k];
                    a++;
                }
            }
        }
    }
    System.out.println(a);
    for(int i=0; i<sa.length; i++){
        for(int j=0; j<3; j++){
            System.out.print(sa[i][j]+" ");
        }
        System.out.println();
    }

The output: 输出:

 12 1 2 12 1 3 11 1 4 10 1 5 9 1 6 8 2 3 10 2 4 9 2 5 8 2 6 7 3 4 8 3 5 7 4 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

To print you are iterating on sa : 要打印,您要在sa上进行迭代:

for(int i=0; i<sa.length; i++){

And at the beginning you stated it has 100 rows ( default values to 0 ): 在开始时,您说它有100行( 默认值为0 ):

int[][] sa = new int[100][3];

It shows those 100 rows independently of what you did on your second for with fa . 它显示了这100行独立的,你做了什么你的第二forfa

Try with: 尝试:

for(int i=0; i<a; i++){
    for(int j=0; j<3; j++){
        System.out.print(sa[i][j]+" ");
    }
    System.out.println();
}

You are printing the content of the entire sa regardless of how many items you have added to it. 无论您添加了多少项,都在打印整个sa的内容。 The first 12 items print correctly, while the remaining items print the initial values of int[3] arrays, which happen to be zeros. 前12个项目可正确打印,而其余项目可打印int[3]数组的初始值,该值恰好为零。

You can fix this by iterating up to a items, like this: 您可以通过迭代a项来解决此问题,如下所示:

for(int i=0; i < a; i++) {
//              ^^^ 
    for(int j=0; j<3; j++){
        System.out.print(sa[i][j]+" ");
    }
    System.out.println();
}

Alternatively, you could switch to using lists in place of arrays. 或者,您可以切换为使用列表代替数组。 Since lists grow dynamically, you would be able to avoid this problem altogether: 由于列表是动态增长的,因此您可以完全避免此问题:

class Triple {
    private final a, b, c;
    public Triple(int a, int b, int c) {this.a=a; this.b=b; this.c=c;}
    public int getA() { return a; }
    public int getB() { return b; }
    public int getC() { return c; }
}

List<Triple> sa = new ArrayList<>();
...
if(fa[i]+fa[j]+fa[k]==15){
    sa.add(new Triple(fa[i], fa[j], fa[k]));
}
...
for (Triple t : sa) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM