2012-05-27 26 views
3

我正在使用二維數組。我想要的是在我的二維數組的特定列中動態添加元素,名稱爲symboltable2。 我一直在做;如何在二維數組中動態添加值?

結果也是一個一維數組,其中有一定的話說:

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

,但上面的代碼沒有給我任何結果...好心幫:(

+0

'字符串[,] symboltable2 =新的字符串[,]'甚至不會編譯:數組創建必須有數組大小或數組初始化器 –

+0

所以它是正確的方式來聲明它? –

+0

因爲我必須將我的symboltable2公開 –

回答

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"; 
1

需要設置所述陣列的大小。並把它公開,我會用一個屬性,並在類的構造函數這樣初始化數組:

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

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

      // ... 
+1

+1。如果可能的話,將其定義爲'public string [,] SymbolTable2 {get;私人設置; }'使用私人setter。 –

+0

這些得到設置嗎? –

+0

我正在工作窗口窗體:( –