【问题标题】:Add array to an array - one by one将数组添加到数组中 - 一个接一个
【发布时间】:2012-07-23 14:41:34
【问题描述】:

我想将数组添加到列表或多维数组(不是一次全部...)。但我真的不明白为什么这会这么难。

假设我有这个:

    string[] a = { "h", "b"};
    string[] b = { "c", "a", "i" };
    string[] c = { "out", "in", "file", "test" };

    ArrayList x = null;

    x.Add(a); //error: Object reference not set to an instance of an object.
    x.Add(b);
    x.Add(c);

我可以用 ArrayList 代替吗

string[,] x = null;

但是没有.Add的选项

假设我有未知数量的大小未知的字符串 [] - 如何将它们添加到列表/多维数组中?再说一遍:我想一一添加这些字符串[]。有什么想法吗?

【问题讨论】:

  • 只是想知道,这是什么ArrayList x = null;
  • 错误“对象引用未设置为对象的实例”,以供将来参考,意味着您忘记初始化对象。

标签: c# .net arrays string list


【解决方案1】:

您收到NullReferenceException,因为您的列表未初始化:

string[] a = { "h", "b"};
string[] b = { "c", "a", "i" };
string[] c = { "out", "in", "file", "test" };

IList<string[]> x = new List<string[]>;

x.Add(a);
x.Add(b);
x.Add(c);

这假设您正在构建一个二维结构。如果您想将数组“展平”为单个字符串列表,请创建一个列表,然后改用其 List.AddRange 方法。

【讨论】:

  • 嗨,谢谢,这很好,但是我如何遍历这个列表?...我只得到 System.String[] 作为输出
  • @miri 你想直接打印列表的元素吗? this link 的答案解释了如何做到这一点。
【解决方案2】:

您还没有创建要存储字符串数组的 ArrayList 实例。尝试添加

ArrayList x = new ArrayList();
x.Add(a);
...
...

【讨论】:

    【解决方案3】:
    ArrayList x = null;
    x.Add(a); 
    

    如果:

    1. 你创建一个ArrayList的实例:

      ArrayList x = new ArrayList();
      

      你所做的只是声明一个局部变量。

    2. 您小心地将ArrayList.AddArrayList.AddRange 分开。前者添加了一个对象。在您的情况下,第一个元素(在第一个 Add 之后)本身就是一个数组。要访问“h”需要x[0][0]AddRange 按术语获取每个传递的集合元素并将其添加到集合中。因此得到“h”将是x[0],而“b”将是x[1]

    我想你想要:

    string[] a = { "h", "b"};
    string[] b = { "c", "a", "i" };
    string[] c = { "out", "in", "file", "test" };
    
    ArrayList x = new ArrayList();
    
    x.AddRange(a);
    x.AddRange(b);
    x.AddRange(c);
    

    【讨论】:

      【解决方案4】:

      关键字null 本质上表示“没有对象”。因此,当您编写 x.Add(a) 时,您是在尝试对不存在的东西调用 Add 方法。

      您需要先初始化您的列表,这会将一些内容放入标有x 的框中:

      ArrayList x = new ArrayList(); 
      

      您现在可以调用x.add(a),您的代码将按预期运行。

      【讨论】:

        【解决方案5】:

        您缺少 ArrayList 的 new,因此您应该这样做:

        ArrayList x = new ArrayList();
            x.AddRange(a);
            x.AddRange(b);
            x.AddRange(c);
        

        你不能在 Add 方法中使用数组,你不会得到任何编译错误,但是当你访问对象时,你只会得到 ToString 类型,这意味着如果你说:

        string[] a = { "h", "b"};
            x.Add(a);
        

        然后尝试遍历以下元素:

        foreach (var item in x)
             {
            Console.WriteLine(item);
             }
        

        你会得到结果:System.String[],我希望你不想这样,所以你需要使用AddRange方法,它接受ICollection类型的参数,所以你说:

        x.AddRange(a);

        如果你在数组列表上做一个循环,比如:

         foreach (var item in x)
                 {
                     Console.WriteLine(item);
                 }
        

        你会得到输出,

        h 
        b
        

        【讨论】:

          【解决方案6】:

          一种方法是:

          List<List<string>> list = new List<List<string>>();
          
              list.Add(new List<string>(){
                  "str1", "str2", "..."
              });
          

          一定要包括:使用 System.Collections.Generic;

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-06-22
            • 2021-12-10
            • 2016-08-16
            • 1970-01-01
            • 2017-12-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多