【问题标题】:How remove array of arraylist using c#?如何使用c#删除arraylist数组?
【发布时间】:2011-07-27 12:03:29
【问题描述】:

如何从数组列表中删除数组

class Program
{
    static void Main(string[] args)
    {
        ArrayList[] arr = new ArrayList[3000];
        int capacityForList = 2;

        int i = 0;
        int j;
        int k;

        arr[i] = new ArrayList();
        arr[i].Add("A");
        i++;
        arr[i] = new ArrayList();
        arr[i].Add("B");
        i++;
        arr[i] = new ArrayList();
        arr[i].Add("C");
        i++;
        arr[i] = new ArrayList();
        arr[i].Add("D");
        i++;
        arr[i] = new ArrayList();
        arr[i].Add("E");
        i++;
    }
}

我想从这里删除 arr[3]

谢谢

【问题讨论】:

    标签: c# arrays c#-3.0 arraylist


    【解决方案1】:

    您不能像从 List<T>ArrayList 中那样从数组中“删除”元素 - 您只能替换元素。诚然,您可以使用 Array.Copy 复制整个数组块,但这有点难看。

    更好的解决方案是将您的代码更改为使用List<List<string>>

    List<List<string>> list = new List<List<string>>
    {
        new List<string> { "A" },
        new List<string> { "B" },
        new List<string> { "C" },
        new List<string> { "D" },
        new List<string> { "E" }
    };
    
    list.RemoveAt(3); // Removes the list containing "D"
    

    但是,每当我看到使用集合集合的代码时,我都会感到紧张……这里的含义是什么?您能否将字符串的“内部”列表更改为更有意义的内容?如果您能给我们提供更多关于大局的信息,那真的很有帮助。

    哦,尽量避免使用像ArrayList 这样的非泛型集合。类型安全很重要:)

    【讨论】:

      【解决方案2】:

      最简单的做法是使用List&lt;ArrayList&gt; 而不是ArrayList[],您可以直接调用arr.RemoveAt(3)

      【讨论】:

        猜你喜欢
        • 2018-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-26
        • 2020-03-06
        • 2021-12-30
        • 2018-04-07
        相关资源
        最近更新 更多