- Home /
How to get the lenght an array?
I have this code:
private Vector3[,] roomLocalization = new[,]
{
{new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,0,0)},
{new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,0,0)},
{new Vector3(0,0,0)}
};
I dont want to get the lenght of one array like "roomLocalization.GetLength(0)" and I need get the number of lines, in this case are 3.There is a command to get this?
Answer by Bunny83 · Dec 13, 2014 at 04:11 PM
Since it's a multidimensional array you have to use GetLength(0) for the first dimension and GetLength(1) for the second.
Btw: You don't initialize your array completely. A multidimensional array can't have different length in a dimension. So a 2d-array is always a rectangle / table. It can't have empty cells. A 3d array is always a cube. It sounds like you think you're using jagged arrays (arrays in arrays) but you don't. A jagged array would look like this:
Vector3[][] myarray;
edit
Here's an example how a jagged array would look like in your case:
private Vector3[][] roomLocalization = new Vector3[][]
{
new Vector3[]{new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,0,0)},
new Vector3[]{new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,0,0)},
new Vector3[]{new Vector3(0,0,0)}
};
Now you have only simple one dimensional arrays. So the "row" count is simply the length of the outer array:
int numRows = roomLocalization.Length;
int numCol0 = roomLocalization[0].Length;
int numCol1 = roomLocalization[1].Length;
Vector3 v = roomLocalization[1][2]; // 3rd element in 2ed "row" / array
Humm Thanks, I am php programmer, maybe that has left me confused :D
Answer by Antony-Blackett · Dec 13, 2014 at 03:13 PM
http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
roomLocalization.Length;
It's giving me 9... I've tried this before... Thank you
Answer by tanoshimi · Dec 13, 2014 at 04:17 PM
"I dont want to get the lenght of one array like "roomLocalization.GetLength(0). I need get the number of lines, in this case are 3"
The number of "lines", 3, is what roomLocalization.GetLength(0)
gives you....
I'm pretty sure he hasn't realized that in his example he only has two dimensions which has rows ( dim 0 / GetLength(0) ) and columns ( dim 1 / GetLength(1) )
Your answer

Follow this Question
Related Questions
Help with arrays, multidimensional 0 Answers
Array exists, but won't give length??? 1 Answer