0

I want to create an Array or a List with N-Dimensions.

Is there a way to do this without having a Method for each possible dimension:

private ____ createArray(int dimensions, int[] lengths)
{
    // declare array with dimensions from variable dimensions
    // set length of first dimension to lengths[0]
    // set length of second dimension to lengths[1]
    // [...] 
    // return array
}

And not like this

private int[][][] create3DArray(int[] lengths)
{
    int[][][] array = new int[lengths[0]][][];

    int[] newLengths = new int[lengths.Count - 1];

    for(int i = 0; i < lengths.Count - 1; i++)
    {
        newLengths[i] = lengths[i + 1];
    }

    for(int i = 0; i < lengths[0]; i++)
    {
        array[i] = create2DArray(newLengths);
    }

    return array;
}

private int[][] create2DArray(int[] lengths)
{
    int[][] array = new int[lengths[0]][];

    for(int i = 0; i < lengths.Count; i++)
    {
        array[i] = new int[lengths[1]];
    }

    return array;
}

If I have to do it like this, I need to have a method for every possible (let's ignore the fact that it may not make sense to have an array with N-Dimensions) amount of dimensions.

5
  • List "dimension" is flexible enough I think, you may just need to initialize its value? Commented Feb 1, 2016 at 9:53
  • 1
    There is the Array.CreateInstance method, but it creates a multi-dimensional array and not a jagged array like you want. Commented Feb 1, 2016 at 9:59
  • @Ian How would I create a List, which I could use like a 4D-Array? Like list[i][j][k][l] = // fill with some random number Commented Feb 1, 2016 at 10:00
  • Checkout my example, is that what you want? Commented Feb 1, 2016 at 10:05
  • @Dirk Array.CreateInstance seems like it could do what i want. :) Commented Feb 1, 2016 at 10:11

3 Answers 3

2

you can create a list of lists:

var myList = new List<List<int>>();
for(int i = 0; i < 10; ++i) {
    var localList = new List<int>();
    myList.Add(localList);
}
Sign up to request clarification or add additional context in comments.

1 Comment

And if I want the List to contain a List which contains a List [...] (as many times as i want) and then return that list?
1

You could create like this using Enumerable.Repeat as many levels as you want:

int x = 5, y = 6, z = 7;
List<List<List<int>>> list3D = Enumerable.Repeat<List<List<int>>>(
    new List<List<int>>(
        Enumerable.Repeat<List<int>>(
            new List<int>(
                    Enumerable.Repeat<int>(0, z)
                    ), y)
        ), x
    ).ToList();
}

In the example above you create 3D lists with initial value of 0 and initial size of 5x6x7

6 Comments

This gets in the right direction. But if i want to make it with N-Dimensions? Instead of 3?
Then just add another level. Notice that the pattern is simply Enumerable.Repeat
...but i have to add another level if i want more dimensions, which is exactly what i don't want.
I get what you mean, in that case you need to have a recursive call. Makes a generic method, then wrap the previous list in a list
This method has a serious flaw in that the inner lists are all identical references. If you set list3D[0][1][2] = 4 then all list3D[x][y][2] == 4.
|
0

It is possible.

Make an int array of dimensions. From there, you can create a conditional var declaration. The only trick is that you need to make explicit the conversion to (Array)

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.