Here's how I understood Java Multidimensional Array length

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Here's how I understood Java Multidimensional Array length

Post by Nipuna » Thu Feb 23, 2012 10:35 pm

Hi.

I am going to share a simple thing that I found today.

I was studying about Java Multidimensional Arrays (aka: 2D Arrays) and I knew it before since I saw Multidimensional Arrays when learning C++. But haven't used it much.

When I was doing studying i found a code like this (This isn't the actual code, I wrote one my myself in this post)

Code: Select all


for (int a = 0; a <array1.length; a++ ){
      for (int b = 0; a <array1[a].length; a++ ){
      System.out.println(array1[a][b]);
      }
}

Note: I didn't add array declaration code here. 


I was able to understand most of it like. The 2 for loops and .length

But I didn't get what these do
array1.length
and
array1[a].length
because they were working in another way and not like what I was thinking.

After experimenting a little, I found out that, Unlike normal arrays, like if we use .length in a normal array (one dimensional array) we get number of elements as the result. But in 2D arrays if we use
array1.length
we get how much rows are there as the result and if we want to find out how much elements in each row we need to use this
array1[a].length


So here's how I understood that :)

Not that much big one, :) But I hope to share for some one who got confused



Thanks
Post Reply

Return to “Java Programming”