1D阵列打印为2D Java

时间:2013-03-14 15:45:13

标签: java object loops arraylist

我正在尝试从数组列表中打印稀疏矩阵,其中列表中的每个对象都包含一对。对(对象)保持两个整数 - 位置和值。 [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。这显示在第二行,它没有整数。对于这篇文章感到抱歉,但我整天都在这一天!

感谢您的回复!

5 个答案:

答案 0 :(得分: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 + " ");
        }
    }

}

答案 1 :(得分:0)

我认为最好对二维数组进行排序(我称之为),然后迭代矩阵的长度。由于不包含任何有关矩阵长度的信息,因此您还需要将其传递到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 + " ");
        }

    }
}

答案 2 :(得分: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("|");
    }
}

答案 3 :(得分: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 

答案 4 :(得分:0)

我总觉得打印2d结构的最简洁方法是嵌套循环:

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