2017-01-19 24 views
0

我想從控制檯應用程序讀取值以將它們添加到二維數組。C#將項目添加到2維數組

當前代碼:

public void getUserResponses(int amount) 
{ 
    int current = 0; 

    string[,] userArray = new string[amount, 2]; // Array 

    while (current < amount) 
    { 
     Console.WriteLine("Please enter username #" + current); 
     string username = Console.ReadLine(); 
     // Add username to array 
     current++; 
    } 
} 

正如你可以在代碼中看到,我想讀的用戶名,然後將其添加到陣列。

例子:

閱讀的用戶名

string username = Console.ReadLine(); 

然後將用戶名添加到陣列中的格式如下:

userArray[username][placeholder] 

我怎麼能管理這個?

+1

爲什麼你有第二維在你的數組? –

+2

考慮使用數據結構和/或字典。 –

+0

要添加兩個值到數組中,第二個尚未創建,但生病需要它以後。 – user7347727

回答

2

您可以通過執行array[i,j] = value;

從你的描述它的感覺是什麼樣一個Dictionary<string, string>可能更相關的訪問二維數組。它將允許你映射字符串與鍵(在你的情況下的名稱)