2

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

位置是整数所在的稀疏矩阵中零的数量。因此,表示为 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。这显示在没有整数的第二行。很抱歉这篇文章,但我整天都在写这篇文章!

感谢您的回复!

4

5 回答 5

0

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

[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 + " ");
        }
    }

}
于 2013-03-14T16:09:12.193 回答
0

我认为您最好对二维数组进行排序(我称它们为您的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 + " ");
        }

    }
}
于 2013-03-14T16:22:30.187 回答
0

像这样的东西,我想...

    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("|");
    }
}
于 2013-03-14T16:27:18.247 回答
0

我试图弄清楚如何解决您当前拥有的问题,但是由于 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 
于 2013-03-14T16:31:18.663 回答
0

我总是觉得打印二维结构最干净的方法是嵌套循环:

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();
    }
}
于 2013-03-14T16:53:10.917 回答