Question:
How can I get the width of a two dimensional array in Java?
quota
2008-02-23 14:08:47 UTC
I know with 1 dimensional array, I can do
int[] array = new int[5];
array.length;

and get 5;

but how about 2 dimensional array

int[] array = new int[5][7]

how can i get its dimensions?
Four answers:
anonymous
2008-02-23 14:16:36 UTC
First off, make sure you you do

int[][] array = new int[5][7], since it's two dimensions.



A two dimensional array is basically an array of arrays. Picture a one dimensional array with 5 elements, where each element is an array of 7 elements itself.



So in the array above, array.length would give you the number of rows - 5, since that's the number of elements in the larger array.



To get the number of columns, just get the length of one of the smaller arrays that serve as the rows. In other words, the first row is actually an array with 7 elements, so array[0].length returns 7. Hope this helps!
?
2015-08-19 07:18:13 UTC
This Site Might Help You.



RE:

How can I get the width of a two dimensional array in Java?

I know with 1 dimensional array, I can do

int[] array = new int[5];

array.length;



and get 5;



but how about 2 dimensional array



int[] array = new int[5][7]



how can i get its dimensions?
?
2016-10-16 12:15:19 UTC
Java Length Of 2d Array
anonymous
2008-02-23 14:12:28 UTC
int[] array = new int[5][7]; creats a new 5x7 array named array


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...