How do you get the width and height of a multi-dimensional array?
You use Array.GetLength with the index of the dimension you wish to retrieve.
How do you get the width and height of a multi-dimensional array if i use it into Jagged Arrays?
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 array10
._EnemyPosition[1].GetLength(0)
which returns the length of the first dimension of the two dimensional array5
_EnemyPosition[1].GetLength(1)
which returns the length of the second dimension of the two dimensional array2
And obviously the result of 2
times 3
is 1
. So, which one do you want?
Size of multi dimensional array
b
is a 2D character array of rows 3
and columns 4
.
So, if you take sizeof(b)
you will get 12
.
b[0]
(and b[i]
in general) has a type of a 1D character array of size 4
. So. if you take sizeof (b[0])
you will get 4
.
b[0][0]
(and b[i][j]
in general) has a type of char
. So if you take sizeof (b[0][0])
you will get 1
.
sizeof
does not depend on the array index. The type remains the same even for b[0]
and b[100]
, even though it might be out of range of the memory of the array.
C: Size of two dimensional array
That's a problem of integer division!
int column = sizeof(result[0])/row;
should be
int column = 7 / 10;
and in integer division, 7/10==0
.
What you want to do is divide the length of one row, eg. sizeof(result[0])
by the size of one element of that row, eg. sizeof(result[0][0])
:
int column = sizeof(result[0])/sizeof(result[0][0]);
How do I find the size of a 2D array?
You want the GetLength() method of your array:
a.GetLength(0);
http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx
Simplify code: height and width of an 2d-array
const valid1 = [ // valid elements [3, 2], [3, 2]]const invalid1 = [ // different sizes [1, 2], [1]]const valid2 = [ // only 1 element [1, 2]]
function sizes(bss) { // avoid empty array (optional) if (!bss.length) { return 'invalid' } // is array same length with other elements const isEqualLength = bss.every(function(v) { return v.length === bss[bss.length - 1].length }) if (isEqualLength) { // get minimal values from each array element const minBss = bss.map(function(v) { return Math.min.apply(null, v) })
return { width: minBss[0], height: bss.length } } return 'invalid'}
// check validconsole.log(sizes(valid1))// check invalid length of elements of arrayconsole.log(sizes(invalid1))// check invalid length of arrayconsole.log(sizes(valid2))
Finding Maximum Height and Width for 2D Array of Unknown Dimensions (Java)
Will .length
still work?
A 2D array is just simply an array, where the items in it are other arrays. Since .length
works on a 1D array, it will surely work on all arrays - the amount of dimensions does not matter.
However, pixels.length
gives you the length of the parent array - i.e. the array that contains all of the other arrays inside it. To get the length of the second dimension, you will have to get the length of the arrays inside it.
So, how do we get the length of the 2nd dimension?
We know that all of these arrays must be the same length, so we use the one at position 0 only because it is the only one that we can be 100% sure will always exist - an array should always have at least one element.
Then, we just get the length of that array - pixels[0].length
.
Getting the array length of a 2D array in Java
Consider
public static void main(String[] args) {
int[][] foo = new int[][] {
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3, 4},
};
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
}
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
Related Topics
Convert Bitmaps to One Multipage Tiff Image in .Net 2.0
Pattern for Calling Wcf Service Using Async/Await
What's the Difference Between Application.Run() and Form.Showdialog()
Multiple Colors in a C# .Net Label
Newtonsoft Add JSONignore at Runtime
Parsing a JSON File with .Net Core 3.0/System.Text.JSON
Textrenderer.Measuretext and Graphics.Measurestring Mismatch in Size
How to Get the Actual Monitor Name? as Seen in the Resolution Dialog
C# When Should I Use List and When Should I Use Arraylist
Returning Http Status Code from Web API Controller
Does Using Tasks (Tpl) Library Make an Application Multithreaded
What Is the Fastest Way I Can Compare Two Equal-Size Bitmaps to Determine Whether They Are Identical
What Is It That Makes Enum.Hasflag So Slow
Generically Flatten JSON Using C#
Programmatically Adding Images to Rtf Document
Retry a Task Multiple Times Based on User Input in Case of an Exception in Task