【问题标题】:list array initialization c#列表数组初始化 c#
【发布时间】:2012-02-19 16:30:49
【问题描述】:
List<authorinfo> aif = new List<authorinfo>();
for (int i = 0; i < 5; i++)
{
    aif.Add(null);
}
aif[0] = new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844);
aif[1] = new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972);
aif[2] = new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844);
aif[3] = new authorinfo("Robinson Crusoe", "Daniel", "Defoe", 1719);
aif[4] = new authorinfo("2001: A Space Odyssey", "Arthur", "Clark", 1968); 
//authorinfo ai = new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844); 
foreach (authorinfo i in aif)
{
    Console.WriteLine(i);
}

好吧,当我删除顶部的 for 循环时,程序不会启动,为什么?因为我需要将 null 添加到列表中。

我知道我做错了。我只希望 aif[0-4] 在那里,我必须添加空对象才能避免出现超出范围的错误是没有意义的。

请帮忙?

【问题讨论】:

  • 除了已经给出的好答案之外,您还可以将一个 int 传递给列表的构造函数以用作列表的容量。

标签: c# arrays list object


【解决方案1】:

只需自己添加新的对象实例:

  List<authorinfo> aif = new List<authorinfo>();
  aif.Add(new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844));
  //... and so on

现在您正在使用 null 作为占位符元素,然后您使用索引器将其覆盖 - 您不必这样做(也不应该这样做)。

作为替代方案,如果您事先知道您的列表元素,您也可以使用collection initializer

  List<authorinfo> aif = new List<authorinfo>()
  {
     new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844),
     new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972),
     new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844)
  };

【讨论】:

  • 谢谢,知道这很容易。
【解决方案2】:

这样做:

var aif = new List<authorinfo> {
        new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844),
        new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972),
        new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844),
        new authorinfo("Robinson Crusoe", "Daniel", "Defoe", 1719),
        new authorinfo("2001: A Space Odyssey", "Arthur", "Clark", 1968)
};

你完成了

【讨论】:

    【解决方案3】:

    当你像这样通过索引访问列表元素时,

    var myObj = foo[4];
    

    您假设集合 foo 至少有五个 (0,1,2,3,4) 元素。您会收到超出范围的错误,因为在没有 for 循环的情况下,您正在尝试访问尚未分配的元素。

    有几种方法可以处理这个问题。最明显的就是使用List&lt;&gt;.Add()

    List<authorinfo> aif = new List<authorinfo>();  
    
    aif.Add(new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844));
    aif.Add(new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972);  
    // ....
    

    不过,对于像这样的玩具(家庭作业)问题,您可以在构造时初始化列表:

    var authorList = new List<authorinfo>
    {  
    new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844)
    ,new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972)  
    , // .....
    };
    

    List&lt;&gt; 和其他集合最有用的一点是它们的大小是动态的,而不是数组。将List&lt;&gt; 视为为您处理所有节点连接的链表。与链表一样,List&lt;&gt; 在添加节点之前没有节点,这是您的 for 循环所做的。在数组中,所有元素的引用空间都是预先分配的,因此您可以立即访问它们,但不能动态修改数组的大小。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2011-06-05
      • 2016-03-08
      • 1970-01-01
      • 2012-10-23
      • 2015-02-07
      相关资源
      最近更新 更多