简体   繁体   中英

C# dynamically add array to array

How can I push an array to another array?

For example, I need to build an array like this one:

var array = [[1, 2, 3], [4], [5, 6], [7, 8, 9, 10]]

This is how I would push an array to another array using javascript :

int loop = 4; // this number can be different
var array = [];
for(var i = 0; i < loop; i++) {
  array.push([i]);
} 

I tried to use lists instead of array as follows:

List<string> finalList= new List<string>();
for(int i = 0; i < loop; i++)
{
  List<string> listHolder = new List<string>();
  listHolder.Add(i);
  finalList.AddRange(listHolder);
}

But after execution the finalList will look like this:

finalList = [1, 2, 3, 4];

Instead of finalList = [[1], [2], [3], [4]]


Every solution is very helpful, but the accepted one helps me the most!

If you want to have an array of arrays, make a list of arrays, and convert it to array:

var listOfArrays = new List<int[]>();
listOfArrays.Add(new[] {1, 2, 3});
listOfArrays.Add(new[] {4});
listOfArrays.Add(new[] {5, 6});
listOfArrays.Add(new[] {7, 8, 9, 10});
var result = listOfArrays.ToArray();

For your second example, the loop would look like this:

var res = new List<int[]>();
for (int i = 1 ; i <= 4 ; i++) {
    res.Add(new[] { i });
}
var arr = res.ToArray();

For the corrected Javascript... In this particular case you have from the beginning the final size of both the containing array ( loop , 4 in this case) and the contained array ( 1 ).

int loop = 4; // this number can be different
int[][] array = new int[loop][];
for (var i = 0; i < loop; i++)
{
    array[i] = new int[] { i };
}

Note that normally you would write it:

int loop = 4; // this number can be different
var array = new int[loop][];
for (var i = 0; i < loop; i++)
{
    array[i] = new[] { i };
}

as other friends suggest to use list the code may be like this

List<List<int>> intArrayList = new List<List<int>>();
                for (int i = 0; i < 5; i++)
                {
                    List<int> intArray = new List<int>();
                    intArray.Add(1);
                    intArray.Add(2);
                    intArray.Add(3);
                    intArrayList.Add(intArray);
                }

The solution is to use a List instead. A list is similar to an array, but with resizable memory that can add more elements later.

List<int[]> listOfArrays = new List<int[]>();
listOfArrays.add(new int[] { 3, 2, 4, 5 });

That way you can keep a list of arrays.

If you want a List of arrays, instead of List<string> finalList= new List<string>() you need to make it's type int[] :

int loop = 4;
List<int[]> finalList = new List<int[]>();
for (int i = 0; i < loop; i++)
{
     finalList.Add(new int[] { i });
}

I would recommend using List<T> like this:

List<List<int>> list = new List<List<int>> { new List<int> { 1, 2, 3 }, new List<int> { 4 }, new List<int> { 5, 6 }, new List<int> { 7, 8, 9, 10 } };
list.Add(new List<int> { 11, 12 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM