如何在2D数组中动态添加值?

时间:2012-05-27 16:38:07

标签: c# winforms

我正在使用2D数组。我想要的是在名为symboltable2的2D数组的特定列中动态添加元素。 我一直这样做;

结果是另一个1D数组,其中有某些单词:

string[,] symboltable2 = new string[,];

if (result.Contains("int")) {
    for (int todynamic = 0; todynamic < result.GetLength(0); todynamic++) {
        symboltable2[todynamic, 6] = "int";
    }
    for (int sym2 = 0; i < symboltable1.GetLength(0); sym2++) {
        f4.listBox6.Items.Add(symboltable1[sym2, 5]); // To show if the values are added or not
    }
} 

但上面的代码并没有给我任何结果......请帮助:(

2 个答案:

答案 0 :(得分:2)

您需要设置数组的大小。为了公开我会使用一个属性并在类构造函数中初始化你的数组,如下所示:

public class MyClass
{
    public string[,] symboltable2 { get; set; } 

    public MyClass()
    {
        symboltable2 = new string[10,10];
    }

            // ...

答案 1 :(得分:0)

在实现数组时,你需要给出数组的维数,即

string[,] sa = new string[5,15];

string[,] sa = new string[listString1.Count, listString2.Count] 

关于向2D数组添加/更改元素..作为简单的字符串数组示例:

sa[0, 1] = "a";
sa[0, 2] = "b";
sa[1, 0] = "Istanbul / Turkey";
相关问题