winforms 在C# winform应用程序中将String[*,*]转换为int[*,*]

6ss1mwsb  于 2022-11-17  发布在  C#
关注(0)|答案(2)|浏览(168)

大家好,我是一个C#编程新手,我想把2D字符串数组转换成2D整型数组,这个转换的原因是我想把整型数组传递给另一个方法进行计算。提前感谢你的帮助。

public void Matrix()
        {
            int a = int.Parse(txtRowA.Text);
            int b = int.Parse(txtColA.Text);

            Random rnd = new Random();
            int[,] matrixA = new int[a, b];

            string matrixString = "";
            
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    matrixA[i, j] = rnd.Next(1, 100);

                    matrixString += matrixA[i, j];
                    matrixString += "    ";

                   
                }
                matrixString += Environment.NewLine;
            }

            

            txtA.Text = matrixString;
            txtA.TextAlign = HorizontalAlignment.Center;

        }
i5desfxk

i5desfxk1#

您的代码实际上非常接近。请尝试以下操作:

private Random rnd = new Random();

public int[,] Matrix(int a, int b)
{
    int[,] matrixA = new int[a, b];
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < b; j++)
        {
            matrixA[i, j] = rnd.Next(1, 100);
        }
    }
    return matrixA;
}

您所拥有的是一个不依赖于任何WinForms控件的函数,它将快速高效地生成您的int[,]
让调用代码与WinForms控件一起工作:

int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
int[,] matrix = Matrix(a, b);

现在,您可以将矩阵传递给需要int[,]的任何对象。
如果您需要显示矩阵,请为此创建一个单独的函数:

public string MatrixToString(int[,] matrix)
{
    StringBuilder sb = new();
    for (int i = 0; i < matrix.GetLength(0); i++)
    {
        string line = "";
        for (int j = 0; j < matrix.GetLength(1); j++)
        {
            line += $"{matrix[i, j]}    ";
        }
        sb.AppendLine(line.Trim());
    }
    return sb.ToString();
}

您的调用代码如下所示:

int a = int.Parse(txtRowA.Text);
int b = int.Parse(txtColA.Text);
int[,] matrix = Matrix(a, b);
UseTheMatrix(matrix);
txtA.Text = MatrixToString(matrix);
txtA.TextAlign = HorizontalAlignment.Center;

样品运行产生:

nlejzf6q

nlejzf6q2#

您可以使用List作为助手来存储从字符串中提取的数组元素,但首先我将字符串中元素之间的空格替换为一个特殊字符'|'做为分隔符号,以便从字串中撷取数字。
您可以执行以下操作:

public static int[,] ConvertToInt(string matrix)
    {
        List<List<int>> initial = new List<List<int>>();

        int lineNumber          = 0;
        int currenctIndex       = 0;
        int lastIndex           = 0;
        int digitCount          = 0;

        initial.Add(new List<int>());

        foreach (char myChar in matrix.ToCharArray())
        {

            if (myChar == '|')
            {
                
                initial[lineNumber].Add(int.Parse(matrix.Substring(lastIndex, digitCount)));
                lastIndex  = currenctIndex+1;
                digitCount = 0;
            }
            else
                digitCount++;

            if (myChar == '\n')
            {
                lineNumber++;
                if(currenctIndex < matrix.Length-1)
                    initial.Add(new List<int>());
            }

            currenctIndex++;
            
        }

        int[,] myInt = new int[initial.Count, initial[0].Count];
        for (int i = 0; i < initial.Count; i++)
            for (int j = 0; j < initial[i].Count; j++)
                myInt[i, j] = initial[i][j];

        return myInt;
    }

相关问题