My problem is I want to initialize a multidimensional array declared like this:
int[][][] my3DArray;
However, the following code gives me an error on [sizeY][sizeZ]
saying it expected ',' or ']'.
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
my3DArray = new int[sizeX][sizeY][sizeZ];
}
When i declare the array like this though:
int[,,] my3DArray;
I am able to initialize it without any problems by doing it like this:
my3DArray = new int[sizeX,sizeY,sizeZ];
But then the problem becomes that if I try to get the length of one of the arrays I can't do for example my3DArray[x,y].Length
and am instead forced to pass all of the indexes together my3DArray[x,y,z]
.
So, is there any way I can initialize the array with it being declared as int[][][] my3DArray;
? Or will I have to store the sizes of the arrays elsewhere and use it like this int[,,] my3DArray;
?
CodePudding user response:
You can do:
new int[][][] myArray = new int[][][]
{
new int[][]
{
new int[]
{
// Your numbers
}
}
}
myArray[0].Length
myArray[0][0].Length
myArray[0][0][0].Length
Works fine
CodePudding user response:
You can't declare a jagged array in one line like this: new int[sizeX][sizeY][sizeZ];
as you really have three levels of nested arrays. You need to initialize them at each level.
This is what that would look like:
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
my3DArray = new int[sizeX][][];
for (var x = 0; x < sizeX; x )
{
my3DArray[x] = new int[sizeY][];
for (var y = 0; y < sizeY; y )
{
my3DArray[x][y] = new int[sizeZ];
}
}
}
Now, if you do want to initialize a multidimensional array, you can do this:
int[,,] z =
{
{ { 1, 2, }, { 1, 2, }, { 1, 2, }, },
{ { 1, 5, }, { 1, 5, }, { 2, 8, }, },
{ { 1, 5, }, { 1, 5, }, { 2, 8, }, },
{ { 1, 5, }, { 1, 5, }, { 2, 8, }, },
};
And then, to get the dimensions, you can do this:
Console.WriteLine(z.GetLength(0));
Console.WriteLine(z.GetLength(1));
Console.WriteLine(z.GetLength(2));
That gives me:
4
3
2
It's important to note that with a jagged array each nested array can be of different length, but with a multidimensional array then each dimension must be uniform.
For example, a jagged array could look like this:
int[][] y = new int[][]
{
new [] { 1, 2, },
new [] { 1, 2, 3, },
new [] { 1, },
};
And a multidimensional array like this:
int[,] y =
{
{ 1, 2, 3, },
{ 4, 5, 3, },
{ 1, 6, 3, },
{ 1, 7, 8, },
};
I could not do this:
//illegal!!!
int[,] y =
{
{ 1, 2, 3, },
{ 4, 5, 3, 7, },
{ 1, 6, },
{ 1, 7, 8, },
};
CodePudding user response:
int[][] is a jaggad array, which means that every array can be in a different size. This is why you can initialize it that way:
int[][] arr2D= new int[3][];
arr2D[0] = new int[0];
arr2D[1] = new int[1];
arr2D[2] = new int[2];
which will create a 2d array that looks like this:
_
_ _
_ _ _
The following is an example to create 3d jaggad array (with different sizes for each dimension- 5 rows, 3 columns and 6 depth):
int[][][] arr3D = new int[5][][];
for (int col = 0; col < arr3D.Length; col )
{
arr3D[col] = new int[3][];
for (int depth = 0; depth < arr3D[col].Length; depth )
{
arr3D[col][depth] = new int[6];
}
}
In order to get the dimension size in jaggad array, you can simply get the lenth of the array, but keep in mind that if the array is actually jagged, you will have different sizes for different arrays. if you initialize all of the arrays in a specific dimension with the same value, than you can check either of them, it will be the same.
int[,] is a multi dimensional array that every dimension have a fixed size.
To create a multi dimensional:
int[,] arr2D = new int[3,4];
which will create a 2d array looking like this:
_ _ _ _
_ _ _ _
_ _ _ _
In multi dimensional array you can't change the length of a specific row or column.
In most cases you will probably prefer a multi dimensional array.
In order to get the size of a specific dimension in a multi dimensional array, you can use the following method:
int rows = arr22.GetLength(0); int cols = arr22.GetLength(1); int depth = arr22.GetLength(2);
The input is the dimension you want the size of.
CodePudding user response:
First,
int[][][]
is a jagged array, or an array of arrays of arrays. Where as,
int [,,]
is a multi-dimensioinal array. A single array with many dimensions.
While multi-dimensional arrays are easier to instantiate and will have fixed dimensions on every axis, historically, the CLR has been optimized for the vastly more common case of single dimensional arrays, as used in jagged arrays. For large sizes, you may experience unexpected performance issues.
You can still get the dimensions of a multi-dimensional array by using the GetLength method, and specifying the dimension you are interested in but, its a little clunky.
The number of dimensions can be retrieved from the Rank property.
You may prefer a jagged array and instantiating one axis at a time.
To instantiate jagged arrays, see this Initializing jagged arrays.
CodePudding user response:
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
my3DArray = Enumerable.Repeat(Enumerable.Repeat(new int[sizeZ], sizeY).ToArray(),sizeX).ToArray();
}