Win a copy of Head First Android this week in the Android forum!
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# 2 dimensional Array!

Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
Hi!
int[][] ar2=new int[1][3];
System.out.println(ar2[1].length);
The above given ArrayIndexOutOfBound Exception.I know that the length of array is 2.Is it not possible to take the length of each dimension?Can anybody explain please?Thanks.

Greenhorn
Posts: 24
• Number of slices to send:
Optional 'thank-you' note:
Hi sridhar,
int[][] ar2=new int[1][3];//Line1
System.out.println(ar2[1].length);//Line2

Array Indexes Start from 0 not 1,so there is no ar2[1] since in line 1 u declared an array of int[1][3]
Line 2 can be replaced by
System.out.pritnln(ar2[0].length);//prints 3

Sridhar Srinivasan
Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
Hi!
If I say
int[][] ar2={{1,2,3},{4,5,6}};//Line1
I can say that ar2[0][1] has value 2 and ar2[1][1] has value 5.
In this case can I find the length of ar2[1]?Am I rite?R something wrong?Please explain.Thanks

Ranch Hand
Posts: 1873
• Number of slices to send:
Optional 'thank-you' note:
Hi Sridhar,
Yes. You are right in your prediction about the length but as earlier noted you probably missed that 0 and 1 indexing logic ....
I guess, if you can refer to the JLS Chapter 10 Arrays then it might make things more clear to you. I as myself didnt refer to JLS much when I took the test but many people have it almost by Heart, to remember from top of my mind -Marlene Miller name comes up...moderators would know it...
Regards
Maulin

Sridhar Srinivasan
Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
In that case, I can say that int[][] ar2=new int[2][2]
The above is similiar to int[][] ar2={{1,2},{3,4}}- rite?
If I am able to get the length of ar2[1].length of the second statement, why can't I get the length of the first one.

Ranch Hand
Posts: 191
• Number of slices to send:
Optional 'thank-you' note:
Hi Sridhar,
I dont think u will have any problem with the first statement.
Even for tht , u can get the length of array using arr2[1].length.It prints 2.
vineela

Sridhar Srinivasan
Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
Hi Vineela!
int[][] ar2=new int[1][3];//Line1
System.out.println(ar2[1].length);//Line2
This shouldn't give any error.rite?Thanks.

Vineela Devi
Ranch Hand
Posts: 191
• Number of slices to send:
Optional 'thank-you' note:
Hi Sridhar,
U r trying to create a multidimensional array.It usually means an array of arrays.

int[][] ar2=new int[1][3];//Line1
System.out.println(ar2[1].length);//Line2

so, here the length of the main array is only 1and length of subarrays is 3. so, as the index starts from 0, there will be only ar[0]. ar[1] doesnot exist at all. so, u will definitely get ArrayIndexOutOfBoundsException.
when u try to create a two dimensional array, say like this:
int array[][] = new int[2][2];
This means tht the length of main array is 2 and the length of each subarray is 2.
in java, since non rectangular arrays can also be defined, u can simply omit the size of the subarrays.
i.e. int array[][] = new int[2][];
is also valid.
The length of subarrays can declared later when we use.
i.e. we can declare as array[0] = new int[2];//Length of first subarray(referred by array[0]) is 2
array[1] = new int [3];
//Length of Second subarray(referred by array[1]) is 3
Hope it is clear to u now,why u r getting the exception.

Vineela

Ranch Hand
Posts: 5093
• Number of slices to send:
Optional 'thank-you' note:
You should get an ArrayIndexOutOfBoundsException on line 2
had you written ar2[0] it would have worked. the array is 1 long, counting from 0, therefore indexing [1] is indexing the 2nd element which doesn't exist.

lowercase baba
Posts: 13014
66
• Number of slices to send:
Optional 'thank-you' note:

int[][] ar2=new int[1][3];//Line1
System.out.println(ar2[1].length);//Line2

when you do this, you have a two dimensional array. we all agree on that.
your 2d array contains 1 1d array. your 1d array contains 3 ints.
in other words ar2 has 1 element (which is an array) in it, at index position 0 -> ar2[0]. that is the ONLY thing in ar2.
so when you say ar2[1], you are past the end of ar2, so you get the error.

Sridhar Srinivasan
Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot for all ur explaination.I understood well.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton