This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes 2D array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2D array" Watch "2D array" New topic
Author

2D array

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


F
4


Lenght returns the number of members that array can have, and in this case it's 12. Why does it return 4?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

In Java, a 2D array is really just an array of arrays. So when you get the length of the outer array, you are simply asking how many inner arrays there are - which is four. If you wanted to find the total amount of values that can be stored in the 2D array you could:
1) If you know each inner array is the same size: Look at the length of values[0], and multiply that by the length of values
2) If you allow that each inner array is independent and can have different sizes, ten you must loop through each index of the outer array, and sum the lengths of the inner arrays


Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Here's a little secret...Java doesn't actually have 2D (or 3D, or 4D...) arrays. It only has 1D arrays. However, an array can contain...arrays!!!

What you have created is a 1d array with four elements, and each of those is holding another array. So your values array really does only have a length of 4.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Java does not have two-dimensional arrays. What you have here is an array of arrays (that's not exactly the same as a two-dimensional array!).

The length is 4 because values is an array of char arrays of length 4. Each of the char arrays have a length 3.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ashish Schottky
Ranch Hand

Joined: Dec 29, 2009
Posts: 93
I agree with all the posts above. However if you still unable to visualize why you get length as 4 rather than 12, then follow this example.
Think your array as 4 empty boxes and place them in one line. How add three boxes in each box. This is what the code [4][3] means.
So now the length of your boxes is 4 and not 12.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2D array
 
Similar Threads
help on printing letter X using *
copying arrarys into a different class with methods
How to retrieve data from database every 10 minutes?
Arrays with files
Convert 1d array to 2D boolean