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"
A two-dimensional array is similar to a one-dimensional array, but it can be visualised as a grid (or table) with rows and columns. For example, a nine-by-nine grid could be referenced with numbers for each row and letters for each column.
Here i and j are the size of the two dimensions, i.e I denote the number of rows while j denotes the number of columns. Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.
You can even create a two-dimensional array where each subarray has a different length or different type, also known as a heterogeneous array in Java. This means it's possible to create a two-dimensional array with variable column length in Java.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With