2

I have a problem on creating a set of list in array

this is my coding but it's wrong one, any correction for this?

List<string>[] item = new List<string>[10]();

I want to create 10 list in string named as item but I can't do it

and then how can I store more than 1 element in each of the 10 item list??

item[1].add(a);     //when I want to print that a I use item[1][0]
item[1].add(b);     //when I want to print that b I use item[1][1]

item[2].add(aa);
item[2].add(bb);

but how can I store element in each of the list?

8
  • You're probably looking for List<string[]> item = new List<new string[10]>(); or something similar Commented Jun 17, 2015 at 3:18
  • It's I can ultilize the list like this? item[1] or item[5]................ Commented Jun 17, 2015 at 3:20
  • No. You'd have to do item[1][1]; or similar syntax. Or just use a simple List<string>, unless you really want a List that contains 10 element string arrays, where each element in the List<T> is a 10 element string array. Commented Jun 17, 2015 at 3:21
  • No, I want 10 list that can contain many element, 10 item with dynamic space Commented Jun 17, 2015 at 3:24
  • A List<T> can contain any number of elements - it's dynamic. Are you saying you want 10 separate instances of a List<T>, or just one with a dynamic number of elements? Commented Jun 17, 2015 at 3:25

2 Answers 2

1

If you KNOW for certain that you want exactly ten lists, you can use an array instantiated to 10 items, each a list of string.

List<string> [] items = new List<string> [10];

Each List is not initialized, so you need to initialize your list before you can use it and each list can be accessed via normal indexer syntax..

if (items[0] == null)
    items[0] = new List<string>();

Once initialized, you can fill in your data.

items[0].Add("another string");

If you wanted to pre-initialize each list so that you do not get a NullReferenceException, do so in a loop.

for (var i = 0; i < items.Length; i++)
    items[i] = new List<string>();

However, if you think that your items may need to hold more List<string> down the road, just simply use a list of lists.

List<List<string>> items = new List<List<string>>();

List wraps array and gives you some nice syntactic sugar and optimizations for expanding arrays which makes your life a lot easier. You can still use indexer syntax to access each list within your list.

if (items[0] == null)
    items[0] = new List<string>();

items[0].Add("another string").
Sign up to request clarification or add additional context in comments.

2 Comments

items[0].Add("another string"); when I use this, it's got error and say that it's null
@GreenHill It's because you are not initializing that list instance. In both techniques I initialize the list instance inside of the parent array/list before using. You cannot miss this crucial step! I also added a quick example of how to initialize each list when using the array parent.
1

As per your comments

"I want 10 separete list that have dynamic space"

You can define your collection as follows.

List<string> [] collection= new List<string> [10];

for(int i=0; i<10; i++)
collection[i] = new List<string>();

or, if you don't care size of the array then you can use this.

List<List<string>> collection = new List<List<string>>(); 

1 Comment

I like the List<List<string>> approach.

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.