在做一个界面实现一个算法(类似于计算器)。不知道如何将文本框中的数据赋值给二维数组,举个例子说明比较直观。
如果在文本框中输入数据:
1 2 3
1 3 6
2 3 8
则a[1][2]=3;a[1][3]=6;a[2][3]=8(每一行的前两个数据是标号,赋给数组下标,第三个数据是权值,赋给数组。文本框中有多少行数据是由用户输入的。)
以前学的是c++,同学介绍用c#做界面比较简单,所以才学c#没几天,语法规则还没全转过来,就遇到了这个问题。。求各位前辈帮帮忙,十分感谢~!

解决方案 »

  1.   

    怎么不用Split呢
    文本框是单行的直接:
     string words = "1,2,3";      string[] split = words.Split(new Char[] { ',' });
    多行的话,先分割每一行,在每一行像上面一样就可以了
      

  2.   


            private void button1_Click(object sender, EventArgs e)
            {
                //已设置textBox1.Multiline=true;
                string[] str = textBox1.Lines;            List<int[]> rowList = new List<int[]>();
                for (int i = 0; i < str.Length; i++)
                {
                    int[] row = str[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                        .Select<string, int>(item => Convert.ToInt32(item)).ToArray();
                    rowList.Add(row);
                }            int rowCount = rowList.Max(row => row[0]) + 1;
                int columnCount = rowList.Max(row => row[1]) + 1;            int[,] datas = new int[rowCount, columnCount];            for (int i = 0; i < rowList.Count; i++)
                {
                    datas[rowList[i][0], rowList[i][1]] = rowList[i][2];
                }
            }这个代码是用逗号(,)来分割的,可以改成其他。