【问题标题】:1D array printed as 2D Java打印为 2D Java 的 1D 数组
【发布时间】:2013-03-03 01:41:04
【问题描述】:

我正在尝试从数组列表中打印一个稀疏矩阵,其中列表中的每个对象都包含一对。对(对象)包含两个整数 - 位置和值。 [pos,val]。

位置是整个数字所在的稀疏矩阵中零的数量。因此,表示为 2d 矩阵,您可能具有例如以下内容:

[000 0023 100] (对不起格式,想象一个 3x3 矩阵)。无论如何,这个数组列表将是

aList = {[5,23], [6,1]}

现在,我使用以下代码尝试遍历它们以创建一个 6x6 矩阵。

public void printFullMatrix() {

    int count = 0;
    int temp = 0;
    for (int i = 0; i < aList.size(); i++) {
        for (int j = 0; j < aList.get(i).pos - temp; j++) {
            count+=1;
            if (count % size == 0){
                System.out.println("");

            } else {
                System.out.print(0 + " ");
            }

        }
        System.out.print(aList.get(i).val + " ");
        temp = aList.get(i).pos;
    }

}

问题是我得到以下打印结果(| 符号表示换行符):

[0 0 35 0 0 99 0 | 0 0 0 0 0 0 | 0 0 0 0 0 0 55| 0 0 20 0 0 0 0 | 0 0 0 3 0 0 0 | 0 0 0 0 0 2 ]

如您所见,第一行有 7 个元素,我发现在每行打印一个整数时,都会添加一个额外的 0。这显示在没有整数的第二行。很抱歉这篇文章,但我整天都在写这篇文章!

感谢您的回复!

【问题讨论】:

  • 您可以为您的示例发布您的输入数组和预期的输出二维数组吗?
  • 我想我对“位置是整个数字所在的稀疏矩阵中零的数量”感到困惑。所以一个 6x6 的例子可能会有所帮助
  • The position is the amount of zeros into the sparse matrix the whole numbers are located 那么条目 [6,1] 没有意义。
  • 预期的输出数组应该是(在 2d 中): [0 0 35 0 99 0 | 0 0 0 0 0 0 | 0 0 0 0 0 55 | 0 20 0 0 0 0 | 0 0 3 0 0 0 | 0 0 0 0 2 0] 输入数组是对象。在上面的示例中,您可能会看到“35”位于数组开头的第二个位置。因此,作为一个对象,这将作为 [2, 35] 输入到数组列表中。其余部分也是如此,但请记住该位置从矩阵的最开始开始 - 而不是行。问题是输出有一个额外的零添加到任何带有整数的行......

标签: java object loops arraylist


【解决方案1】:

好的,您的代码存在一些问题,我假设您的输入是:

[2, 35] [3, 99] [17, 55] [19, 20] [26, 3] [34, 2]

首先,当您添加一个非零数字时,您不会增加计数,这就是为什么您在其他数字的行上获得额外的零。

然后,您需要对新行进行更多检查(即,对于在行的开头或结尾添加非零数字的情况)。

完成所有这些之后,您需要添加矩阵末尾剩余的任何零。

以下代码应该适合您:

public void printFullMatrix() {

    int count = 0;
    int temp = 0;
    int rows = 0;

    for (int i = 0; i < aList.size(); i++) {
        if (count % size == 0) {
            System.out.println("");
            rows++;
        }
        for (int j = 0; j < aList.get(i).pos - temp; j++) {
            count += 1;
            if (count % size == 0) {
                System.out.println("");
                rows++;
            } else {
                System.out.print(0 + " ");
            }

        }
        System.out.print(aList.get(i).val + " ");
        temp = aList.get(i).pos;
        count++;
        if (count % size == 0) {
            System.out.println("");
            rows++;
        }
    }

    while (rows < size) {
        count++;
        if (count % size == 0) {
            System.out.println("");
            rows++;
        } else {
            System.out.print(0 + " ");
        }
    }

}

【讨论】:

  • 感谢大家花时间帮助/实际编写函数!非常感谢!这项任务的一小部分和花费的时间如此之多,全部为 8%!再次感谢各位。安德烈。
【解决方案2】:

我认为最好对二维数组进行排序(我称它们为 pairs),然后遍历矩阵的长度。由于 pairs 不包含有关矩阵长度的任何信息,因此您还需要将其传递给您的 printFullMatrix

另外一个警告:因为你永远不会有两对具有相同数量的零索引(例如,{{5,3}, {5,4}} 永远不会发生),那么你只需要迭代最多一次每个矩阵条目的配对列表。

这是我的做法:

import java.util.Arrays;
import java.util.Comparator;

public class q15413815 {

    public static void main(String[] args) {
        Integer[][] pairs = { { 6, 1 }, { 5, 23 } };
        printFullMatrix(pairs, 3);
    }

    public static void printFullMatrix(Integer[][] pairs, int length) {
        // Sort the pairs so we can simply iterate through them.
        Arrays.sort(pairs, new Comparator<Integer[]>() {

            @Override
            public int compare(Integer[] a1, Integer[] a2) {
                return a1[0] - a2[0];
            }

        });

        // Walk through the matrix and populate it with values.
        for (int i = 0; i < length * length; i++) {
            if (i % length == 0 && i != 0) {
                System.out.println();
            }

            int value = 0;
            for (int j = 0; j < pairs.length; j++) {
                if (pairs[j][0] == i) {
                    value = pairs[j][1];
                }
                if (pairs[j][0] > i) {
                    break;
                }
            }

            System.out.print(value + " ");
        }

    }
}

【讨论】:

  • 感谢大家花时间帮助/实际编写函数!非常感谢!这项任务的一小部分和花费的时间如此之多,全部为 8%!再次感谢各位。安德烈。
【解决方案3】:

我想是这样的......

    int[][] lll = new int[][] { {5, 23}, {6, 1}, {14, 6}};

    int count = 0;
    final int MATRIX_SIZE = 6;
    int index = 0;
    for (int pos = 0; pos < lll.length; pos++) {
        count = lll[pos][0];
        for (int i = 0; i < MATRIX_SIZE; i++) {
            index++;

            if (count == index - 1) {
                System.out.print(lll[pos][1] + " ");
            }
            else {
                System.out.print(0 + " ");
            }
        }
        System.out.print("|");
    }
}

【讨论】:

  • 感谢大家花时间帮助/实际编写函数!非常感谢!这项任务的一小部分和花费的时间如此之多,全部为 8%!再次感谢各位。安德烈。
【解决方案4】:

我试图弄清楚如何解决您当前拥有的问题,但是由于 for 循环不会打印出最后一个对象的值之后的任何 0,我只是从一个 while 循环重新开始,该循环将始终打印出矩阵。这假定您的列表将始终按位置排序。

使用{[2,35], [4,99], [17,55], [19,20], [26,3], [34,2]}的输入列表

int count = 0;
int currentObj = 0; // the object whose value is going to be printed
int numObjs = 6; // the number of objects to print
int size1 = 6; // number of rows
int size2 = 6; // number of positions in a row

// make sure you loop through every possible position
// the way you have it now stops after "2" is printed, leaving out the last "0"
while (count < size1 * size2) {
    count++; // increment the position every loop

    // if there are still objects to print out,
    // and if count is at the position of the current object
    //  pos of the objects is actually the number of positions before that value
    //  so pos is technically (position to print number - 1)
    if (currentObj < numObjs && (count - 1) == aList.get(currentObj).pos) {
        System.out.print(aList.get(currentObj).val + " ");
        currentObj++; // remember to increment to the next object

    // not at the position of an object's value, so print 0
    } else {
        System.out.print("0 ");
    }

    // go to the next line if size2 positions have been printed
    if (count % size2 == 0) {
        System.out.println("");
    }
}

输出:

0 0 35 0 99 0 
0 0 0 0 0 0 
0 0 0 0 0 55 
0 20 0 0 0 0 
0 0 3 0 0 0 
0 0 0 0 2 0 

【讨论】:

  • 感谢大家花时间帮助/实际编写函数!非常感谢!这项任务的一小部分和花费的时间如此之多,全部为 8%!再次感谢各位。安德烈。
【解决方案5】:

我一直觉得打印二维结构最简洁的方法是嵌套循环:

public void printFullMatrix() {
    int mi = 0; // matrix index
    int li = 0; // list index
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            System.out.printf(" %3d", 
               (li < aList.size() && mi++ == aList.get(li).pos)
               ? aList.get(li++).val : 0);
        }
        System.out.println();
    }
}

【讨论】:

  • 感谢大家花时间帮助/实际编写函数!非常感谢!这项任务的一小部分和花费的时间如此之多,全部为 8%!再次感谢各位。安德烈。
猜你喜欢
  • 1970-01-01
  • 2017-07-29
  • 2018-12-23
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2020-12-18
相关资源
最近更新 更多