Creating an array of two-dimensional arrays in C#

半腔热情 提交于 2021-02-08 12:21:07

问题


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"


回答1:


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.




回答2:


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 ^_^




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/34851509/creating-an-array-of-two-dimensional-arrays-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!