我可以编写一个函数来打印多维数组的内容并将其 returns 作为字符串打印出来吗?

Can I write a function that prints out the contents of a multidimensional array and returns it as a string?

所以我写了一个名为 Afficher(int [] m) 的函数,它以类似 table 的方式打印出多维数组的内容。问题是我希望函数 return 是一个字符串而不是一个空值,这样我可以稍后将它组合到另一个字符串。有没有办法做到这一点,也许通过使用 StringBuilder? 谢谢

        static void Afficher(int[,] m)
        {

            string lignePleine = new string('-', m.GetLength(1) * 6 + 1);

            for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
            {

                Console.WriteLine(lignePleine);
                Console.Write("|");
                for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
                {

                    {
                        Console.Write($"{m[ligne, colonne],3}  |");
                    }
                }
                Console.WriteLine();
            }

            Console.WriteLine(lignePleine);
            Console.WriteLine();


        }


        public int[,] CreationMatrice()
        {
            Random generateur = new Random();
            List<int> NewValeur = new();
            for (int i = 1; i <= (Trajet.NB_LIGNES * Trajet.NB_COLONNES); i++)
            {
                listeValeurs.Add(i);
            }
            int nombreValeur = listeValeurs.Count;
            for (int i = 0; i < nombreValeur; i++)
            {
                int indexRandom = generateur.Next(listeValeurs.Count);
                NewValeur.Add(listeValeurs[indexRandom]);
                listeValeurs.RemoveAt(indexRandom);
            }

            int index = 0;
            for (int ligne = 0; ligne < Trajet.NB_LIGNES; ligne++)
            {
                for (int colonne = 0; colonne < Trajet.NB_COLONNES; colonne++)
                {
                    matrice[ligne, colonne] = NewValeur[index];
                    index++;
                }
            }
            return matrice;
        }

您可以不打印组成字符串的部分,只需将它们添加到一个对象中,然后 return 它在末尾:

static string Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6 + 1);
    var result = "";
    for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
    {         
        result += lignePleine + "\n" + "|";

        for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
        {
            result += $"{m[ligne, colonne],3}  |";
        }
        result += "\n";
    }
    result += lignePleine + '\n';
    return result;
}

https://onlinegdb.com/G9ErwOcE6

以上简单添加字符串,或者如果您愿意,使用 String.Concat:

static string Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6 + 1);
    var result = "";
    for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
    {         
        result = string.Concat(result, lignePleine + "\n" + "|");

        for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
        {
            result = string.Concat(result, $"{m[ligne, colonne],3}  |");
        }
        result = string.Concat(result, "\n");
    }
    result = string.Concat(result, lignePleine + '\n');
    return result;
}

然后您可以调用它,例如,使用您的其他函数:

public static void Main(string[] args)
{
    Console.WriteLine(Afficher(CreationMatrice()));
}

https://onlinegdb.com/bR2G_UYwb

这只是 2 种方法,有几种,这取决于目标和最终目标,您可以在这里找到有关此事的更多信息:

Most efficient way to concatenate strings?

这里是 StringBuilder,与其他方法没有太大区别:

static String Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6 + 1);

    StringBuilder sb = new StringBuilder();
    for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
    {
      sb.AppendLine(lignePleine);
      sb.Append("|");
      for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
      {
        sb.Append($"{m[ligne, colonne],3}  |");
      }
      sb.AppendLine();
    }
    sb.AppendLine(lignePleine);
    sb.AppendLine();
    return sb.ToString();
}