miri
miri

Reputation: 1691

Add array to an array - one by one

I would like to add arrays to a list or multidimensional array (not all at once...). But I dont really understand why this should be that hard.

Lets say I have this:

    string[] a = { "h", "b"};
    string[] b = { "c", "a", "i" };
    string[] c = { "out", "in", "file", "test" };

    ArrayList x = null;

    x.Add(a); //error: Object reference not set to an instance of an object.
    x.Add(b);
    x.Add(c);

Can I use instead of the ArrayList maybe

string[,] x = null;

But there is no option to .Add

Lets say I have an unknown amount of string[]´s with an unknown size - how do I add them to a List/multidimensional array? And again: I would like to add these string[]´s one by one. Any ideas?

Upvotes: 1

Views: 500

Answers (6)

Embedd_0913
Embedd_0913

Reputation: 16565

You are missing new for ArrayList so you should do it like this:

ArrayList x = new ArrayList();
    x.AddRange(a);
    x.AddRange(b);
    x.AddRange(c);

You can't can't use array in Add method , you will not get any compile error but then while you will access the object you will get just ToString on type that means if you say:

string[] a = { "h", "b"};
    x.Add(a);

and then try to loop through the elements like:

foreach (var item in x)
     {
    Console.WriteLine(item);
     }

you will the result: System.String[] and I hope you don't want that, so you need to use AddRange method which takes an argument of type ICollection, so you say:

x.AddRange(a);

and if you do a loop on array list like:

 foreach (var item in x)
         {
             Console.WriteLine(item);
         }

you will get the output,

h 
b

Upvotes: 1

John Bonfardeci
John Bonfardeci

Reputation: 476

One way to do it is:

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

    list.Add(new List<string>(){
        "str1", "str2", "..."
    });

be sure to include: using System.Collections.Generic;

Upvotes: 0

Chris Taylor
Chris Taylor

Reputation: 47402

The keyword null essentially means "no object". Therefore when you write x.Add(a) you are trying to call the Add method on something that isn't there.

You need to initialize your list first, which puts something in the box labelled x:

ArrayList x = new ArrayList(); 

You can now call x.add(a) and your code will work as expected.

Upvotes: 1

Richard
Richard

Reputation: 109180

ArrayList x = null;
x.Add(a); 

That would work if:

  1. You create an instance of ArrayList:

    ArrayList x = new ArrayList();
    

    all you were doing was to declare a local variable.

  2. You are careful to separate ArrayList.Add from ArrayList.AddRange. The former adds a single object. In your case the first element (after the first Add) would be itself an array. To access the "h" would need x[0][0]. AddRange takes each passed collection element in term and adds it to the collection. Thus getting the "h" would be x[0] and "b" would be x[1].

I think you want:

string[] a = { "h", "b"};
string[] b = { "c", "a", "i" };
string[] c = { "out", "in", "file", "test" };

ArrayList x = new ArrayList();

x.AddRange(a);
x.AddRange(b);
x.AddRange(c);

Upvotes: 1

J.B
J.B

Reputation: 1700

you haven't made an instance of the ArrayList you want to store the string arrays in. Try adding

ArrayList x = new ArrayList();
x.Add(a);
...
...

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

You are getting an NullReferenceException because your list is not initialized:

string[] a = { "h", "b"};
string[] b = { "c", "a", "i" };
string[] c = { "out", "in", "file", "test" };

IList<string[]> x = new List<string[]>;

x.Add(a);
x.Add(b);
x.Add(c);

This assumes that you are constructing a 2-D structure. If you would like to "flatten" your arrays into a single list of strings, create a list, and use its List.AddRange method instead.

Upvotes: 6

Related Questions