Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Getting dimensions of Array[][]

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does Array.length work when you have a 2 dimensional array??
Haven't been able to find this anywhere.
Thanks in advance.
e
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give it a try...
See the code snippet below.

public class test
{
public static void main ( String[] args)
{
int a[][] = {{10,20,30,40},{300,400,300}};
int s = a.length;
System.out.println("The array dimension is "+String.valueOf(s));
for (int i=0;i<s;i++)>
System.out.println("The length is "+String.valueOf(a[i].length));
}
}
The first print statement outputs 2 which means that it is a two dimensional array.(a.length =2)
The second and third print statements in the loop outputs, 4&3..
a[0].length = 4
a[1].length = 3
which is the length of the array at each level (0 and 1).
hope it is clear...
Kalidas
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric
Kalidas has displayed the answer to your question very nicely. I'll just add some generalities here.
In Java, arrays are one-dimensional. In order to create a two-dimensional array you are essentially creating a one dimensional array that contains a one-dimensional array in each of the first array's elements.
Put another way, in Java a two-dimensional array is simply an array of arrays. This is why Java allows the second dimension of a "two-dimension" array to have different numbers of elements. And Kalidas has demonstrated this nicely in his above example where the first dimension of array a contains an array of four elements, whereas the second dimension of array a contains three elements.
So, keeping in mind that Java arrays are one dimensional, the answer to your question is that Array.length returns the length of whatever you've specified to be "Array", whether it be the base array, a, or one of its elements, a[0] or a[1].
Perhaps the question is better stated as, "How do you obtain the number of elements in a two-dimensional array?" Presumably you know the answer to that now.
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much!
I had just started thinking about this possibility today when I was reading up on XML. There is an XML datatype you can use to embed java code, which is nested with double brackets. They mentioned the only way you could confuse it was with the following syntax:
array[array[10]]> x
The string "]]>" is used to close the tag. Hmmmmm.... I said to myself.... haven't seen THAT syntax before!
The problem with the example is that it's a two-dimensional array, but one of those dimensions is of length 2, so it's not entirely clear.
If I declare an array myArray[5][4] there are in fact 20 elements, right? Not 9. Wouldn't its length be 5?
Well, now that I know the concept, I'll try some tests.
Thanks!
[This message has been edited by eric moon (edited December 17, 2000).]
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!