{"id":27,"date":"2009-01-21T08:52:48","date_gmt":"2009-01-21T13:52:48","guid":{"rendered":"http:\/\/blog.buluschek.com\/?p=27"},"modified":"2023-07-02T10:49:24","modified_gmt":"2023-07-02T08:49:24","slug":"getting-the-number-of-dimensions-of-a-multidimensional-array-in-java","status":"publish","type":"post","link":"https:\/\/www.buluschek.com\/?p=27","title":{"rendered":"Getting the number of dimensions of a multidimensional array in Java"},"content":{"rendered":"<p>Based on my [permalink href=&#8221;4&#8243;]previous post[\/permalink] about cloning multidimensional arrays, I have come up with a short static function to get the number of dimensions which a multidimensional array has.<\/p>\n<p>Note that this method only returns the number of dimensions of the original object passed as argument. It does not expand into the contained objects themselves. Example:<\/p>\n<p>[sourcecode language=&#8217;java&#8217;]<br \/>\nObject[] a = new Object[3][0];<br \/>\nSystem.out.println(getNumberOfDimensions(a));<br \/>\n[\/sourcecode]<br \/>\nprints 2.<\/p>\n<p>[sourcecode language=&#8217;java&#8217;]<br \/>\nObject[] a = new Object[1];<br \/>\na[0] = new float[2][5][1];<br \/>\nSystem.out.println(getNumberOfDimensions(a));<br \/>\n[\/sourcecode]<br \/>\nprints 1.<\/p>\n<p>Here&#8217;s the code:<br \/>\n[sourcecode language=&#8217;java&#8217;]<br \/>\n\/**<br \/>\n * Returns the number of dimensions which a multidimensional<br \/>\n * array has as a positive integer. For example, returns 3<br \/>\n * when passed a <code>float[][][]<\/code><br \/>\n * @param src the multidimensional array<br \/>\n * @return the number of dimensions of src<br \/>\n * @author Philipp Buluschek bulu@romandie.com<br \/>\n *\/<br \/>\npublic static int getNumberOfDimensions(Object[] src){<br \/>\n  int dim = 1;<br \/>\n  Class cl = src.getClass().getComponentType();<br \/>\n  while(cl.isArray()){<br \/>\n    dim++;<br \/>\n    cl = cl.getComponentType(); \/\/ will not return null as we tested isArray() above<br \/>\n  }<br \/>\n  return dim;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Based on my [permalink href=&#8221;4&#8243;]previous post[\/permalink] about cloning multidimensional arrays, I have come up with a short static function to get the number of dimensions which a multidimensional array has. Note that this method only returns the number of dimensions of the original object passed as argument. It does not expand into the contained objects [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[8,4,7,27,6],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-java","tag-array","tag-code","tag-dimensions","tag-java","tag-multidimensional-array"],"_links":{"self":[{"href":"https:\/\/www.buluschek.com\/index.php?rest_route=\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.buluschek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buluschek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buluschek.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buluschek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27"}],"version-history":[{"count":11,"href":"https:\/\/www.buluschek.com\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":344,"href":"https:\/\/www.buluschek.com\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/344"}],"wp:attachment":[{"href":"https:\/\/www.buluschek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buluschek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buluschek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}