I have an Array where i want to know each array length.
public int[][,] _EnemyPosition = new int[][,]
{
new int[,]{ {0},{1},{2},{3},{4}},
new int[,]{ {0,6},{1,7},{2,8},{3,9},{4,10}},
};
Debug.Log(_EnemyPosition.Length); // Output is 2
Debug.Log(_EnemyPosition[1].GetLength(0)); // Output is 5
But i am not able to get
Debug.Log(_EnemyPosition[1][0].length);
Its throw me an error
error CS0022: Wrong number of indexes `1' inside [], expected `2'
i want to know how to get length of this array
_EnemyPosition[1][0].length
var length = _EnemyPosition[0].Length; //returns 5 (5·1)
var length = _EnemyPosition[1].Length; //returns 10 (5·2)
Is that what you want? Your requirement is far from clear:
i want to know how to get length of this array:
_EnemyPosition[1][0].length
That is not an array, _EnemyPostion[1]
is a two dimensional array [,]
. Consider the following analogue scenario:
int[,] myTwoDimensionalArray = ...
var whatever = myTwoDimensionalArray[0]; //not valid, array dimensiones don't match.
So, we have three options when it comes to the length you want returned:
_EnemyPosition[1].Length
which returns the total length of the two dimensional array 10
. _EnemyPosition[1].GetLength(0)
which returns the length of the first dimension of the two dimensional array 5
_EnemyPosition[1].GetLength(1)
which returns the length of the second dimension of the two dimensional array 2
And obviously the result of 2
times 3
is 1
. So, which one do you want?
Let's disentangle the arrays: you have
int[][,] _EnemyPosition
which is an array of 2d arrays ; so you can call
_EnemyPosition.Length
which is number of 2d arrays in total; for each 2d array at index
position you can call
_EnemyPosition[index].GetLength(0)
_EnemyPosition[index].GetLength(1)
which are the lengths of each dimensions (lines and columns) within index
s 2d array. Please notice, that jagged array and 2d array are different types; that's why you can't put
// doesn't compile
_EnemyPosition[1][0]
since _EnemyPosition[1]
returns 2d array which wants 2 indexes (something like _EnemyPosition[1][0,1]
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.