【问题标题】:Additional values in 2d array二维数组中的附加值
【发布时间】:2017-03-30 11:24:29
【问题描述】:

我在 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 中,它给了我以下结果:

02000
12040
12005
00000
00000

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

【问题讨论】:

  • edit您的问题添加一个标签,表明您正在使用哪种编程语言。
  • 这也是学习如何使用调试器的好时机

标签: java arrays multidimensional-array


【解决方案1】:

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

代码:

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;
    }
}

【讨论】:

  • 打印 0 的数组
  • 为什么会出现 1,0 和 1,1 的值?
【解决方案2】:
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]
  • 我需要了解您的任务。请描述你的代码应该做什么。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多