简体   繁体   English

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

[英]Convert String[*,*] into int[*,*] in C# winform application

Hello everyone I am new to programming in c#, what I am going to do is that I want to convert the 2D string array to 2D integer array, the reason for this conversion is that I want to pass that integer array to another method for some calculation.大家好,我是 c# 编程的新手,我要做的是将 2D 字符串数组转换为 2D integer 数组,此转换的原因是我想将 Z157DB7DF530023572E8ZB 数组传递给另一种方法 for someC666计算。 Thanks in advance for helping.提前感谢您的帮助。

        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;


        }

Your code is actually pretty close.您的代码实际上非常接近。 Try this:尝试这个:

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;
}

What you have there is a function that does not rely on any WinForms controls and will produce your int[,] quickly and efficiently.您所拥有的是 function,它不依赖于任何 WinForms 控件,并且会快速有效地生成您的int[,]

Let the calling code work with the WinForms controls:让调用代码与 WinForms 控件一起工作:

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

Now you can pass the matrix to anything that requires an int[,] .现在您可以将矩阵传递给任何需要int[,]的东西。

If you need to display the matrix then create a separate function for that:如果您需要显示矩阵,请为此创建一个单独的 function:

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();
}

Your calling code would look like this:您的调用代码如下所示:

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;

A sample run produces:示例运行产生:

矩阵

You can use List as a helper to store the elements of the array extracted from the string, but first I replaced the SPACE between elements in your string with a special character '|'您可以使用List作为帮助器来存储从字符串中提取的数组元素,但首先我将字符串中元素之间的空格替换为特殊字符“|” as a separator to make it easier to extract the numbers from the string.作为分隔符,以便更容易地从字符串中提取数字。

you can do somthing like this:你可以这样做:

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;
    }

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

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