简体   繁体   English

c# output 列表<list<> > 到控制台</list<>

[英]c# output List<List<>> to the console

I want to test the function. and output List<List> to the console我想测试 function. 和 output List<List> 到控制台

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<int>> MoveLeftForawrd(List<int> Gameboard, int Y, int X)
            {
                  . . .

                return PossibleMoves;
            }
            
            List<List<int>> test = new List<List<int>> { };
            List<int> Gameboard = new List<int> { };
            int Currentplayer = 1;
            List<int> gameboard = new List<int> {
                -1,0,-1,0,-1,0,-1,0,
                0,-1,0,-1,0,-1,0,-1,
                -1,0,-1,0,-1,0,-1,0,
                0,0,0,0,0,0,0,0,
                0,0,0,0,0,0,0,0,
                0,1,0,1,0,1,0,1,
                1,0,1,0,1,0,1,0,
                0,1,0,1,0,1,0,1

                    };

            Gameboard = gameboard;

            test = MoveLeftForawrd(Gameboard, 5, 7);

            test.ForEach(Console.WriteLine);


        }
    }
}

but all I get is in the console..但我得到的只是在控制台中..

System.Collections.Generic.List`1[System.Int32]

tell me, how do I correctly output list<list> to the console?告诉我,我如何正确地将 output list<list> 发送到控制台? I will be very grateful for your help.我将非常感谢你的帮助。

You have a list of lists:你有一个列表列表:

List<List<int>> test = new List<List<int>> { };

So you could iterate over each list, and then iterate over the items in that list:因此,您可以遍历每个列表,然后遍历该列表中的项目:

var count = 0;

foreach(var outerItem in test)
{
    foreach(var innerItem in outerItem)
    {
        if(count>0)
        {
            Console.Write(",");
        }

        Console.Write($"{innerItem}");
        if(count++ == 8)
        {
           Console.WriteLine();
           count = 0;
        }
    }
}

You can try using string.Join and pinch of Linq in order to get string from List<string> :您可以尝试使用string.Join和捏合Linq以从List<string>获取string

List<List<int>> PossibleMoves = ...

...

string report = string.Join(Environment.NewLine, PossibleMoves
  .Select(line => string.Join(" ", line.Select(item => $"{item,3}")));

Console.WriteLine(report);

Here we join items in each line with space:在这里,我们用空格连接每行中的项目:

string.Join(" ", line.Select(item => $"{item,3}"))

eg {1, 0, -1, 0} will be " 1 0 -1 0"例如{1, 0, -1, 0}将是" 1 0 -1 0"

and then join lines with Environment.NewLine to get something like然后加入Environment.NewLine行以获得类似

 1  0 -1  0
-1  0  0  0
 0 -1  1 -1

You can do this simply using layered foreach loops eg您可以简单地使用分层 foreach 循环来做到这一点,例如

    List<List<int>> Lists = new List<List<int>>(){new List<int>(){1,2,3,4,5},
    new List<int>(){6,7,8,9,0},
    new List<int>(){1,2,3,4,5}};
    
    foreach(var list in Lists)
    {
        foreach(var c in list)
        {
            Console.Write($" {c} ");
        }
        Console.WriteLine("");
    }

Output: Output:

 1  2  3  4  5 
 6  7  8  9  0 
 1  2  3  4  5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM