• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

size of 2D array

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
How can I find out length of a 2d array that I get as a parameter of a function?
If I have:
public void getArray(String[][] arr){
}
How can I find out what is the size of each dimension just by knowing from getArray() that this is a two-dimensional array?
thanks,
Alex
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,
A Java 2D array is really a 1D array of 1D arrays -- i.e., there's a "backbone" array whose elements are 1D arrays, whose elements are (in your example) Strings. So if the array variable is named "arr", then "arr.length" is one dimension, the length of the backbone. The other dimension can technically be different for every element of the backbone -- the array doesn't have to be "rectangular," it can be "ragged." But if you know that it is rectangular, then the other dimension is just "arr[0].length".
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic