时间:2019-03-17 标签:c#outputList>>到控制台

发布于 2025-01-18 16:55:51 字数 1199 浏览 3 评论 0 原文

我想测试功能。和输出列表< 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);


        }
    }
}

但我得到的只是在控制台中。

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

告诉我,如何正确输出列表&lt; list&gt;到控制台?我将非常感谢您的帮助。

I want to test the function. and output List<List> to the console

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? I will be very grateful for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_畞蕅 2025-01-25 16:55:51

您有一个列表列表:

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

因此您可以迭代每个列表,然后迭代该列表中的项目:

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 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(
quot;{innerItem}");
        if(count++ == 8)
        {
           Console.WriteLine();
           count = 0;
        }
    }
}
情未る 2025-01-25 16:55:51

您可以尝试使用 string.join linq 的捏合,以获取 String list&lt; string&lt; string&gt;

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

...

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

Console.WriteLine(report);

在这里,我们将每行的项目加入空间:

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

例如 {1,0,-1,0} 将为“ 1 0 -1 0”

,然后与< code> emoverition.newline 获得类似的东西

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

You can try using string.Join and pinch of Linq in order to get string from List<string>:

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

...

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

Console.WriteLine(report);

Here we join items in each line with space:

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

e.g. {1, 0, -1, 0} will be " 1 0 -1 0"

and then join lines with Environment.NewLine to get something like

 1  0 -1  0
-1  0  0  0
 0 -1  1 -1
完美的未来在梦里 2025-01-25 16:55:51

您可以简单地使用分层 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("");
    }

输出:

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

You can do this simply using layered foreach loops e.g.

    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(
quot; {c} ");
        }
        Console.WriteLine("");
    }

Output:

 1  2  3  4  5 
 6  7  8  9  0 
 1  2  3  4  5 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文