Creating an array of two-dimensional arrays in C#
Asked Answered
E

4

14

I simply want to create an array of two dimensional arrays to store coordinate points.

So I want an array where each index returns a two dimensional array which I would use as x and y.

Here's what I've tried:

waypoints = new int[4][,]    {
        {{0},{6, 0}},
        {{1},{1, 1}},
        {{2},{1, 5}},
        {{3},{6, 5}}
    };

I realize this probably looks stupid, but I've tried looking it up on Google, and I have not gotten any good results.

It gives an error:

"error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead "

I also tried doing it like this:

waypoints = new int[4][,] {
        new int[,] {6, 0},
        new int[,] {1, 1},
        new int[,] {1, 5},
        new int[,] {6, 5}
        };

This gives an error:

"error CS0846: A nested array initializer was expected"

Epizootic answered 18/1, 2016 at 9:41 Comment(3)
Possible duplicate of Omitting c# new from jagged array initializationCassandry
Why not create a list of Vector2 structs or something like that? It may be easier to work with and simpler in the long run.Airtight
@rhughes: Not to mention better locality of reference (an important consideration for the L-n caches in current processors) and reduced garbage collection (as arrays are an object and structs are not).Thaddeusthaddus
F
13

One more curly bracket set {} is required in the initial declaration:

var waypoints = new int[4][,]   {
    new int[,] {{6}, {0}},
    new int[,] {{1}, {1}},
    new int[,] {{1}, {5}},
    new int[,] {{6}, {5}}
};

This is because for such to 2D array, each element in the array is considered as an array for the initialization (albeit it is typically used per element of the array such as val[0,0] = 4;).

Edit (after feedback from comments):

Put in contrast with int[][] (known as jagged array, that is: array of arrays whose array member can be of different size), int[,] is a 2D array with fixed dimension. Both are array which stores arrays, and therefore each element of the array is an array. This explains why there is a need to put another curly bracket in its initialization as above.

Such 2D array, when initialized differently, will result in different dimension (and thus there are multiple ways to initialize it):

int[,] val = new int[,] { { 6 }, { 0 } }; //resulting in int[2,1]
int[,] val = new int[,] { { 6, 0 } }; //resulting in int[1,2]

In either way, additional set of curly bracket is needed.

For the differences between jagged array and multidimensional, fixed sized, array, there are already plenty good explanations/benchmarking available online from well reputed sources. And I understand that it wouldn't be significant, apart from the OP's interest, for me to put more info about it. (And thus the scope for this answer is originally directed only to answer the failed initialization).

It is understood that the solution is not best used for storing coordinate points (as done by OP). The explanation above is given to explain why his initialization doesn't work, rather than to provide best solution for storing coordinate points. As for storing coordinate points, Point in the System.Drawing struct will be more proper (as suggested in the comment).

The use of 2D array to represent single point in 2D Cartesian coordinate is an "overkill", as a 1D array is already capable of storing as many numbers as the computer allows, much more than two numbers required to store points in Cartesian coordinate system.

Familiarize answered 18/1, 2016 at 9:45 Comment(4)
Thanks! If you could explain why that is i would be happy :)Epizootic
I believe this is best explained by Microsoft in the MSDN: social.msdn.microsoft.com/Forums/en-US/… PS: but if you want to store point, it may be worth to store it using Point classFamiliarize
I believe new int[,] {{6,0}} would suffice and would arguably be more readableCassandry
@aardila yes, since it is two dimensional, this is truly overkill. We may even only need int[] (and we can already store more than 2 numbers). My example simply shows that another set of curly bracket is needed (for the 2D array). But for working with geometrical point, I think Point would work best - see other answer.Familiarize
P
12

There's a simple answer to this. Use instances of the "System.Drawing.Point" class. Create a "Point" array to store coordinates. To create the array:

Point[] points = new Point[4];

And to assign a value to the array (e.g. at position 0) use the following code.

points[0] = new Point(xvalue,yvalue);//Where "xvalue" and "yvalue" are integer variables.

And to get the X and Y values from an instance of the point class. Use the below code.

int xvalue = points[0].X;

int yvalue = points[0].Y;

P.S. You can use these points to assign locations to Controls, but that's another story ^_^

Parashah answered 18/1, 2016 at 9:48 Comment(0)
T
2

The arrays are two-dimensional and have unknown size, the initializer can define arrays of various sizes:

        var waypoints = new int[4][,]   {
                new int[,] { {1, 2, 6}, {3, 4, 5} }, // two rows, 3 columns
                new int[,] { {1, 1}, {1, 2} } , // 2 x 2
                new int[,] { {1, 5} }, // 1 x 2
                new int[,] { {6, 5} }
                };
Theolatheologian answered 18/1, 2016 at 9:59 Comment(0)
C
0

I may be misunderstanding the question, but aren't you going a dimension too far in your arrays? This is sort of the same point of view as the guys suggesting to use a Point or Vector class. If each array is only storing one point (which it might not be, I may just be assuming that), you don't need 3 dimensions. You could have an array of regular arrays, and use index 0 for the x coordinate and index 1 for the y coordinate.

It seems to me you're creating an entire 3rd dimension in which each array is size 1, which would be much simpler to visualize as a 2d array. Though it kind of looks like you're using the arrays as a sort of key-value pair, could the index itself be used as the key? They seem to be incrementing each time anyway.

Chunchung answered 28/1, 2020 at 21:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.