7

I have a problem where I need an array of arrayList.

For example if we take an Array of ArrayList of int, it will be like:

int[]<List> myData = new int[2]<List>;

myData[0] = new List<int>();
myData[0].Add(1);
myData[0].Add(2);
myData[0].Add(3);


myData[1] = new List<int>();
myData[1].Add(4);
myData[1].Add(5);
myData[1].Add(6);

myData[0].Add(7);

How can we implement a datastructure like the above in C#?

In C, its like a array of LinkedList. How can I do the same in C#?

4
  • 3
    The example is not an array of ArrayList. Commented Feb 1, 2011 at 15:39
  • int[]<List> makes no sense sintactically. Maybe you want List<int>[]? Commented Feb 1, 2011 at 15:42
  • for your next question concerning C#, I would recommend to tag it with C#, because otherwise people may not find it. Commented Feb 1, 2011 at 15:53
  • I know that the syntax is not supported in C# hence the question - How to do it in a different way. Thanks for C# tag recommendation. I will surely remember it. Commented Feb 3, 2011 at 20:06

5 Answers 5

15
var myData = new List<int>[]
{
    new List<int> { 1, 2, 3 },
    new List<int> { 4, 5, 6 }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Qrystal, This is the solution I needed.
You don't need "List<int>" -- var myData = new[] { new List<int> { 1, 2, 3 }, new List<int> { 4, 5, 6 } };
10

Almost as you tried, only the first line is incorrect:

List<int>[] myData = new List<int>[2];
myData[0] = new List<int>();
myData[0].Add(1);
myData[0].Add(2);
myData[0].Add(3);


myData[1] = new List<int>();
myData[1].Add(4);
myData[1].Add(5);
myData[1].Add(6);

myData[0].Add(7);

Thanks to madmik3, here is a link you can read something about generic lists in C#: click me

Also, if you want to read something about arrays, e.g. the static copy method of the Array class, here is some link for that.

1 Comment

This answer is right but the asker should also take a little time to earn more about Collections and Generics in C#. You will use the all the time so learning them will help a ton. You can start with list like above here: msdn.microsoft.com/en-us/library/6sh2ey19.aspx
4
var arraySize = 2;
var myArray = new List<Int32>[arraySize];


myArray[0] = new List<Int32>();
myArray[1] = new List<Int32>();
// And so on....

myArray[0].Add(5);

1 Comment

Thanks Chris, This is the solution I needed.
3

I prefer lists but it's up to you...

List<List<int>> lst = new List<List<int>>();

lst.Add(new List<int>());
lst.Add(new List<int>());

lst[0].Add(1);
lst[1].Add(1);
lst[1].Add(2);
lst[0].Add(5);

Then if you really want a list at the end of it all use some linq.

lst.ToArray();

2 Comments

C# is case sensitive, and List<T> methods (like everything in the BCL) are PascalCased.
Bah, copy and paste typo. Thanks for fixing it
1

You're trying to take the concrete type List<int> and make an array of it.
Just like string becomes new string[2], so to List<int> becomes new List<int>[2].

This will create an array which can hold two List<int>s.
However, each element in the array starts out null.
You'll need to put a new List<int>() into each slot of the array before using it.


However, you should probably use a List<List<int>> instead of an array of lists.

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.