2015-02-08 105 views
1

我想创建一个列表数组,其中将包含一个字符串和数组列表。创建一个列表数组

例如: 我要赞一个。

列表(0)---字符串值
列表(0)---列表(O) - 字符串值
列表(0)----列表(1) - 字符串值

列表(1)---字符串值
列表(1)---列表(O) - 字符串值
列表(1)----列表(1) - 字符串值

依此类推。

我将如何申报?

我想:

List<List<String>> list = new List<List<string>>(); // but it didn't work. 
List<string[]> arrayList = new List<string[]>(); // again it didn't work.. 

这是可能要申报吗? 如果是这样怎么样?

+0

你是什么意思没有工作?首先声明至少是有效的。 – 2015-02-08 11:56:43

+0

我甚至都不知道您希望如何存储数据。 – 2015-02-08 11:58:12

回答

0

正如我可以在你的数据结构看你问一个包含两个ListList和所有的人都一样string型的,那么你应该去Dictionary。作为单一类型的一个List罐,您可以一次向它添加单个值。尝试Dictionary

Dictionary<string, string> dictionary = new Dictionary<string, string>(); 

,或者如果你想包含stringList一个Dictionary

Dictionary<List<string>, List<string>> dictionary = new Dictionary<List<string>, List<string>>(); 
1

心不是这个Dictionary<string, string[]>

var x = new Dictionary<string, string[]>(); 

x.Add("string1", new string[] {"a", "b", "c"}) 

然后你可以有一个该字典的列表。

var list = new List<Dictionary<string, string[]>>(); 

list.Add(x); 
0

这是否适合您?

public class Tree<T> : List<Tree<T>> 
{ 
    public Tree(T value) { this.Value = value; } 
    public T Value { get; set; } 
} 

它不是一个数组,而是一个列表,但它有很多相同的结构。

然后,您可以指定它是这样的:如果你想创建一个字符串列表的数组,在代码中使用第二种方式

var trees = new [] 
{ 
    new Tree<string>("Branch 1") 
    { 
     new Tree<string>("Leaf 1.1"), 
     new Tree<string>("Leaf 1.2"), 
    }, 
    new Tree<string>("Branch 2") 
    { 
     new Tree<string>("Leaf 2.1"), 
     new Tree<string>("Leaf 2.2"), 
    }, 
}; 
0

试试这个

 List<List<String>> str_2d_list = new List<List<String>>(); 
     List<String> l1 = new List<string>(); 
     l1.Add("l1.string1"); 
     l1.Add("l1,string2"); 
     List<String> l2 = new List<string>(); 
     l2.Add("l2.string1"); 
     l2.Add("l2,string2"); 
     str_2d_list.Add(l1); 
     str_2d_list.Add(l2); 
0

。但如果你想在代码中使用第一种方法列表。

using System; 
using System.Collections.Generic; 

namespace ConsoleApplication1 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      // an example of list of strings 
      List<string> names = new List<string>(); 
      names.Add("Mike"); 
      names.Add("Sarah"); 
      List<string> families = new List<string>(); 
      families.Add("Ahmadi"); 
      families.Add("Ghasemi"); 

      // 1st way 
      List<List<string>> outsideList = new List<List<string>>(); 
      outsideList.Add(names); 
      outsideList.Add(families); 


      // 2nd way 
      Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 
      d.Add("first", names); 
      d.Add("second", families); 

      // how to access list<list<>> 
      foreach (List<string> list in outsideList) 
      { 
       foreach (string s in list) 
        Console.WriteLine(s); 
      } 

      // how to access list inside dictionary 
      foreach (List<string> list in d.Values) 
      { 
       foreach (string s in list) 
        Console.WriteLine(s); 
      } 
     } 
    } 
}