如何为多个二维数组制作干净的 toString

How to make a clean toString for multiple 2 dimensional array

我有一个 ArrayList,其中包含一些对象,该对象具有一个名为 grid 的属性,该属性是用数字填充的二维数组。

我想像“网格的大网格”一样并排打印网格,由一个字符分隔,而不是逐行并且最多应该有 6 个数组相邻。 我试图逐行打印前 6 个数组的行,然后转到下一行,但我迷路了,而且无法正常工作。

我想拥有这个

但是像这样

到目前为止,这是我所做的:

public String toString() {
    String s = "";
    int count = 0;

    for (int i = 0; i < square.size(); i++) {

        for (int k = 0; k < 5; k++) {
            
            for (int j = 0; j < 5; j++) {
                

                if(square.get(i).getGrid()[k][j] >= 10){
                    if(j == 0){
                        s+="| "+String.valueOf(square.get(i).getGrid()[k][j])+" ";
                    }
                    if(j == 4){
                        s+=String.valueOf(square.get(i).getGrid()[k][j])+" |";
                    }
                    if(j!= 0 && j !=4){
                        s+=String.valueOf(square.get(i).getGrid()[k][j])+" ";
                    }
                }

                else{
                    if(j == 0){
                        s+="| "+String.valueOf(square.get(i).getGrid()[k][j])+"  ";
                    }
                    if(j == 4){
                        s+=String.valueOf(square.get(i).getGrid()[k][j])+"  |";
                    }
                    if(j!= 0 && j !=4){
                        s+=String.valueOf(square.get(i).getGrid()[k][j])+"  ";
                    }
                }

            }
            count++;

            if(count == 6){
                count = 0;
                s += "\n";
            }
        }


    }

    return s;

计数器是我想要并排排列的数组的数量。

有更好的方法吗?

看起来你已经完成了大部分的工作,我们只需要构建每一行,我们可以通过稍微修改你现有的代码来使用我们可以追加的字符串列表来做到这一点go 而不是单个字符串,格式化起来会很痛苦。我们可以合并 return 最后的单个字符串:

//Create a list of lines to print so that we can append to the end of each line as we go
ArrayList<String> linesToPrint = new ArrayList<>();

//Populate the list with blank strings
for (int i = 0; i < square.size(); i++)
{
    linesToPrint.add("");
}

//Loop through the list of squares
for (int i = 0; i < square.size(); i++)
{
    //Put the count inside the loop so it resets every time
    int count = 0;
    //loop throughthe rows
    for (int k = 0; k < 5; k++)
    {
    //Create a blank string to store the current grid row
    String row = "";
    //loop through the columns
    for (int j = 0; j < 5; j++)
    {
        //Format double didgits
        if (square.get(i).getGrid()[k][j] >= 10)
        {
        if (j == 0)
        {
            row += "| " + String.valueOf(square.get(i).getGrid()[k][j]) + " ";
        }
        if (j == 4)
        {
            row += String.valueOf(square.get(i).getGrid()[k][j]) + " |";
        }
        if (j != 0 && j != 4)
        {
            row += String.valueOf(square.get(i).getGrid()[k][j]) + " ";
        }
        }
        //Format single didgets
        else
        {
        if (j == 0)
        {
            row += "| " + String.valueOf(square.get(i).getGrid()[k][j]) + "  ";
        }
        if (j == 4)
        {
            row += String.valueOf(square.get(i).getGrid()[k][j]) + "  |";
        }
        if (j != 0 && j != 4)
        {
            row += String.valueOf(square.get(i).getGrid()[k][j]) + "  ";
        }
        }
    }
    
    //Once the row has been created store it in the list
    linesToPrint.set(count, (linesToPrint.get(count)) + row);
    count++;
    }
}

//Construct the complete string
String s = "";
for (int i = 0; i < square.size(); i++)
{
    s += linesToPrint.get(i) +"\r\n";
}
return s;

现在 returned 字符串应该可以正常工作了。

请注意,这只有在使用等宽字体的控制台时才能正确显示。

需要在遍历列表时逐行构建显示。 例如:Line1 = List1Line1 + List2Line1 + etc 使用辅助列表:result 保留行(主要是每个有序元素)。

测试列表中填充了虚拟值,因此每个列表包含来自 1-25 的随机顺序的元素。简单的输出检查是检查单个列表中的元素以查看是否所有元素都满足一个组 1-25.

public class TestArrayPrint {

    static List<Integer>[] list =  new ArrayList[5];
    static List<String> result = new ArrayList<String>();
    public static void main(String[] args) 
    {
        //dummy values 
        for(int i=0;i<list.length;i++)
        {
            list[i] = IntStream.rangeClosed(1, 25).boxed().collect(Collectors.toList());
            Collections.shuffle(list[i]);
        }
        //populate list[0] from array
    
        int dummy_arr[][] = {{1,2,3,4,5},
                             {6,7,8,9,10},
                             {11,12,13,14,15},
                             {16,17,18,19,20},
                             {21,22,23,24,25}};
        list[0].clear();
        for(int i=0;i<dummy_arr.length;i++)
        {
            for (int j=0;j<dummy_arr[i].length;j++)
            {
                list[0].add(dummy_arr[i][j]);
            }
        }
        //full scan
        for(int k=0;k<list.length;k++)
        {
            //next line
            for(int i=0;i<list.length;i++)
            {
                //add first line from first list, then first line from second list, etc
                for(int j=0;j<list.length;j++)
                {
                    //covert to 2 string digits for display 
                    String s= (list[i].get(0)<10) ? " "+list[i].get(0) : list[i].get(0).toString() ;
                    list[i].remove(0);
                    //elements will be in order 
                    //Line1List1 Line1List2 ...
                    //Line2List1 Line2List2 ...
                    result.add(s);
                }
            }
        }
        //result.forEach(System.out::println);
        System.out.println("Ordered Elements to Display="+result.size()+"\n");
        
        //display
        //just adapt i,j,k for various matrix sizes
        for(int i=0;i<5;i++)
        {
            StringBuffer line = new StringBuffer();
            for(int j=0;j<5;j++)
            {
                for(int k=0;k<5;k++)
                {
                    if(k==0)
                    {
                        line.append("| "+result.get(0));
                        result.remove(0);
                    }
                    else if(k==4)
                    {
                        line.append(" "+result.get(0)+" |");
                        result.remove(0);
                    }
                    
                    else
                    {
                        line.append(" "+result.get(0));
                        result.remove(0);
                    }
                }
            }
            System.out.println(line);
        }   
    }
}

输出

Ordered Elements to Display=125

| 11 12  5 13 15 ||  1 24 19 22 13 ||  6  9  8  3 13 || 18  3 10 14  2 || 19 21  3 23 18 |
| 20 24 21  3  7 || 12 14  7  8 16 || 11  4 23  1  5 ||  6  8 13 23 15 || 20 12  7 13  6 |
|  8 18  1 25  2 ||  9  3  2 25 17 || 12 17 21 20 16 || 12 24 17 20 22 || 10  1 16 15  8 |
| 23 22  4 17 16 || 10 21 18  4 11 || 25 24 14 22  2 ||  7  4  5  9 19 ||  9  5 14 11  2 |
|  9 10 19 14  6 || 23 15  5 20  6 || 18 10  7 19 15 || 11 16 21  1 25 || 24 17 25 22  4 |

注意:如果您的列表具有获取特定元素的方法,则无关紧要,该机制应该类似(删除已处理的元素甚至对您的逻辑也很有用)。在我的示例中,元素按行流动,因此 element(6) : list.get(5)list.grid(1,0).