0

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[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

2 Answers 2

2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.