2012-10-11 63 views
2

使用列表可以說我有這樣的一個數組(我知道這是不可能在C#):如何在這個例子中

string[,] arr = new string[,] 
{ 
    {"expensive", "costly", "pricy", 0}, 
    {"black", "dark", 0} 
}; 

所以,我怎麼可以在列表中添加這個項目,我怎麼可以添加「pricy」之間的新項目0?我在網上找不到任何例子。

+0

看到這個問題。 http://stackoverflow.com/questions/5375989/add-item-to-a-jagged-array –

回答

6

數組是不可變的,所以你不能真正添加​​或刪除它們的項目。例如,您可以做的唯一事情就是將項目複製到另一個數組實例,減去不想要的項目,或者執行相同操作,但使用更高維度並添加需要添加的項目。

我建議在這裏使用List<T>,其中T可能是一種簡單的類型,它反映了您要添加到陣列中的事物。例如:

class Thing { 
    public string Prop1 {get; set; } 
    public string Prop2 {get; set; } 
    public string Prop3 {get; set; } 
    public int Prop4 {get; set; } 
} 

List<Thing> list = new List<Thing>(); 

list.Add(new Thing() { Prop1 = "expensive", Prop2 = "costly", Prop3 = "pricy", Prop4 = 0}; 

然後你可以插入項目:

如果
list.Insert(1, new Thing() { Prop1 = "black", Prop2 = "dark", Prop4 = 0}); 

不知道這會爲你工作,這取決於是否能夠使你的「鋸齒」的數據以適應Thing。很明顯,這裏的'Prop1'等將是你在數組中的數據的實際屬性名稱。

5

如果你想添加(插入)項目,那麼不要使用數組。使用List<>

你的樣品可能被

var data = new List<string>[2] { new List<string>(), new List<string>() }; 

覆蓋然後可以使用之類的語句

data[0].Add("expensive"); 
string s = data[1][1];  // "dark" 

這當然是不可能的有0一個字符串數組或列表。您可以使用null,但要儘量避免它。

0

你可以使它成爲Dictionary<string,string>,但關鍵必須保持唯一。然後你就可以遍歷像這樣

Dictionary<string,string> list = new Dictionary<string,string>(); 
foreach(KeyValuePair kvp in list) 
{ 
    //Do something here 
} 
1

那麼你有什麼想要的清單?現在你已經有了字符串和整數所以object是你的公共基類

你可以做一個交錯數組(數組的數組):

object[][] arr = new [] 
{ 
    new object[] {"expensive", "costly", "pricy", 0}, 
    new object[] {"black", "dark", 0} 
}; 

或列表的列表:

List<List<object>> arr = new List<List<object>> 
{ 
    new List<object> {"expensive", "costly", "pricy", 0}, 
    new List<object> {"black", "dark", 0} 
}; 

但這兩者似乎都是不好的設計。如果你提供更多關於你想要完成的信息,你可能會得到一些更好的建議。

0

你的任務有點奇怪,我不明白它的用處。但在你的情況下,你可以不用List等等。您應該通過索引器來查看元素(在您的示例中,string [,]中的項只能使用兩個索引獲取)。

所以在這裏解決方案這樣的作品,我做到了只爲有趣

var arr = new[,] 
          { 
           {"expensive", "costly", "pricy", "0"}, 
           {"black", "dark", "0", "0"} 
          }; 
      int line = 0; 
      int positionInLine = 3; 
      string newValue = "NewItem"; 


      for(var i = 0; i<=line;i++) 
      { 
       for (int j = 0; j <=positionInLine; j++) 
       { 
        if (i == line && positionInLine == j) 
        { 
         var tmp1 = arr[line, positionInLine]; 
         arr[line, positionInLine] = newValue; 

         try 
         { 
          // Move other elements 
          for (int rep = j+1; rep < int.MaxValue; rep++) 
          { 
           var tmp2 = arr[line, rep]; 

           arr[line, rep] = tmp1; 

           tmp1 = tmp2; 

          } 
         } 
         catch (Exception) 
         { 

          break; 
         } 
        } 
       } 
      } 
0
class Program 
{ 
    static void Main(string[] args) 
    { 
     List<Array> _list = new List<Array>(); 

     _list.Add(new int[2] { 100, 200 }); 
     _list.Add(new string[2] { "John", "Ankush" }); 

     foreach (Array _array in _list) 
     { 
      if (_array.GetType() == typeof(Int32[])) 
      { 
       foreach (int i in _array) 
        Console.WriteLine(i); 
      } 
      else if (_array.GetType() == typeof(string[])) 
      { 
       foreach (string s in _array) 
        Console.WriteLine(s); 
      } 
     } 
     Console.ReadKey(); 
    } 
}