2d数组中的附加值

时间:2017-03-30 11:24:30

标签: java arrays multidimensional-array

我在Java(1.8.0.121)中创建了以下代码,用于将iMatrix数组解析为matrixOutput。 一旦我运行它,我注意到在单元格1,0和1,1中出现了额外的值。

有人可以建议我如何更改以避免其他阵列的这些添加(我计划一个通用代码,它将采用不同的阵列,如iMatrix阵列)     公共课Dbg {

private static int[][] matrixOutput = { { 0, 2, 0, 0, 0 },
                                        { 0, 0, 0, 4, 0 }, 
                                        { 1, 2, 0, 0, 5 }, 
                                        { 0, 0, 0, 0, 0 },
                                        { 0, 0, 0, 0, 0 } };
private static int[][] iMatrix = {
        {1, -1, 0, 0, 0},
        {0, 1, 0, -1, 0},
        {-1, 0, 1, 0, 0},
        {0, -1, 1, 0, 0}, 
        {0, 0, 1, 0, -1},
        {0, 0, 0, 2, 0}};
private static int i = 0;
private static int j = 0;
private static int val = 0;

public static void main(String[] args) {

    int[][] list = new int[5][5];

    for (int m = 0; m < iMatrix.length; m++) {
        for (int n = 0; n < iMatrix[m].length; n++) {
            if (iMatrix[m][n] == 1){
                i = n;
            }
            if (iMatrix[m][n] == -1){   
                j = n;
                val = n + 1;

            }
            list[i][j] = val;
        }
    }

    for (i = 0; i < list.length; i++) {
        for (j = 0; j < list.length; j++) {
            System.out.print(list[i][j]);
        }
        System.out.println();
    }

}
}

MatrixOutput的Insead它给了我以下结果:

02000
12040
12005
00000
00000

当我改变3行和4行的位置时,我看到1,0中的值1消失了。来自iMatrix的所有值都正确存储。

3 个答案:

答案 0 :(得分:0)

在创建列表时,在第二个for循环内初始化val = 0。因为您的代码会打印以前的值&#39; val&#39;如果iMatrix [m] [n]不等于&#39; 1&#39;或者&#39; -1&#39;。

代码:

for (int m = 0; m < iMatrix.length; m++)
{
    for (int n = 0; n < iMatrix[m].length; n++) 
    {
        val = 0;
        if (iMatrix[m][n] == 1)
        {
            i = n;
        }
        if (iMatrix[m][n] == -1)
        {   
            j = n;
            val = n + 1;
        }
        list[i][j] = val;
    }
}

答案 1 :(得分:0)

private static int[][] matrixOutput = { 
        { 0, 2, 0, 0, 0 },
        { 0, 0, 0, 4, 0 }, 
        { 1, 2, 0, 0, 5 }, 
        { 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0 } };
private static int[][] iMatrix = { 
    { 1, -1, 0, 0, 0 },
    { 0, 1, 0, -1, 0 },
    { -1, 0, 1, 0, 0 },
    { 0, -1, 1, 0, 0 },
    { 0, 0, 1, 0, -1 },
    { 0, 0, 0, 2, 0 } };


public static void main(String[] args) {

    int[][] list = new int[5][5];

    for (int m = 0; m < iMatrix.length; m++) {
        int i = 0;
        int j = 0;
        int val = 0;
        for (int n = 0; n < iMatrix[m].length; n++) {

            if (iMatrix[m][n] == 1) {
                i = n;
            }
            if (iMatrix[m][n] == -1) {
                j = n;
                val = n+1;

            }
            list[i][j] = val;
        }
    }

    for (int i = 0; i < list.length; i++) {
        for (int j = 0; j < list.length; j++) {
            System.out.print(list[i][j]);
        }
        System.out.println();
    }

}

答案 2 :(得分:0)

通过添加打印验证和变量标记来重写我的代码。

private static int[][] iMatrix = {
        {1, -1, 0, 0, 0},
        {0, 1, 0, -1, 0},
        {-1, 0, 1, 0, 0},
        {0, -1, 1, 0, 0}, 
        {0, 0, 1, 0, -1},
        {0, 0, 0, 2, 0}};
private static int i = 0;
private static int j = 0;
private static int val = 0;
private static int flag = 0;

public static void main(String[] args) {

    int[][] list = new int[5][5];

    for (int m = 0; m < iMatrix.length; m++) {
        for (int n = 0; n < iMatrix[m].length; n++) {
            if (iMatrix[m][n] == 1){
                i = n;
                flag++;
            }
            if (iMatrix[m][n] == -1){   
                j = n;
                val = n + 1;
                flag++;
            }

            if (flag == 2){
                list[i][j] = val;
                flag = 0;
            }


        }
    }

    for (i = 0; i < list.length; i++) {
        for (j = 0; j < list.length; j++) {
            System.out.print(list[i][j]);
        }
        System.out.println();
    }

}

输出变得如下:

02000
00040
12005
00000
00000