简体   繁体   中英

Printing entire array in C#

I have a simple 2D array:

int[,] m = { {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} };

How can I print this out onto a text file or something? I want to print the entire array onto a file, not just the contents. For example, I don't want a bunch of zeroes all in a row: I want to see the

{{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0} };

in it.

Just iterate over it and produce the output. Something like

static string ArrayToString<T>(T[,] array)
{
    StringBuilder builder = new StringBuilder("{");

    for (int i = 0; i < array.GetLength(0); i++)
    {
        if (i != 0) builder.Append(",");
        builder.Append("{");

        for (int j = 0; j < array.GetLength(1); j++)
        {
            if (j != 0) builder.Append(",");
            builder.Append(array[i, j]);
        }

        builder.Append("}");
    }

    builder.Append("}");

    return builder.ToString();
}

没有标准的方法来获取那些{括号,你必须在迭代数组并将它们写入文件时将它们放入代码中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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