2

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!

4
  • 5
    Use a list instead of an array Commented Apr 4, 2017 at 10:54
  • @TimSchmelter I've tried, editing the question right now Commented Apr 4, 2017 at 10:55
  • What @TimSchmelter said. Having a powerful structure like List<>, using arrays is nonsense when you know the number of elements is going to change Commented Apr 4, 2017 at 10:56
  • @xanatos I corrected the javascript code. Commented Apr 4, 2017 at 11:00

6 Answers 6

2

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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 };
}

Comments

1

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.

Comments

1

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);
                }

2 Comments

I think the name arrayList is a bit not correct because that reminds one about the System.Collections.ArrayList class.
Ok , one can follow the proper naming convention, I have updated the variable name as you suggested
1

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 });
}

Comments

0

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 });

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.