开发者

How remove array of arraylist using c#?

开发者 https://www.devze.com 2023-03-23 11:30 出处:网络
How to remove the array from arraylist class Program { static void Main(string[] args) { ArrayList[] arr = new ArrayList[3000];

How to remove the array from arraylist

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开发者_StackOverflow中文版[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++;
    }
}

from this i want remove arr[3]

Thank you


You can't "remove" elements from an array in the same way that you can from a List<T> or an ArrayList - you can only replace elements. You can copy a whole chunk of an array using Array.Copy, admittedly, but it's a bit ugly.

A better solution would be to change your code to use a 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"

However, whenever I see code using collections of collections, I get nervous... what's the meaning here? Could you perhaps change the "inner" list of strings to something more meaningful? If you could give us more information about the bigger picture, it would really help.

Oh, and try to avoid using the non-generic collections like ArrayList. Type safety is important :)


A simplest thing to do is use List<ArrayList> instead of ArrayList[], you can them simply call arr.RemoveAt(3)

0

精彩评论

暂无评论...
验证码 换一张
取 消