• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Questions

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Consider the following code
<code>class Test
{
public static void main(String[] args)
{
int i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}
</code>
the answer is 4,3,3
can somebody explained how the compiler evaluate this result
------------------
coffee drinker
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit mawkin:
Hi All,
Consider the following code
<code>class Test
{
public static void main(String[] args)
{
int i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}
</code>
the answer is 4,3,3
can somebody explained how the compiler evaluate this result


If i understand correctly, its evaluated from left to right
Initial value of i will be 4
and once i becomes 3 subsequent values of i gets changed as well.
In a multi-dimensional matrix,
int [3][] //Second value is not mandatory and the length is 3
Same applies here
int a[4][3][3]//Length is 4
Please correct me if i am wrong
Thankx
Ragu
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ragu,
Thanx for your reply
but still not clear to me
its ok that while saying
<code>
i=4;
int x[][][]=new int[i][i=3][i];
</code>
that third bracket will have a value of 3 fine
but look at its print statement
<code>
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
</code>
generally speaking the length of an array represents no of elements in it .
so if i say
<code>
int a[][]=new int[4][3];
</code>
it means array will hold 4*3 12 elements
also in java Correct me if I am wrong multidimensional arrays are implemented as arrays of arrays.
so when we say ia.length why we get 4 instead of 4*3*3 But as we are reffering to whole arrays not a a subarray inside it.
I can very well get your point that in a 2 dimensional arrays the second part could be left intentionally blank .
I am weak in multidimensional arrays,so if this sort of question comes in my scjp i am definitely goona have to use hit and try method.

[This message has been edited by amit mawkin (edited November 19, 2001).]
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three-dimensional arrays are treated like 2 dimensional. For example, a three-dimensional array of ints could be created with the declaration statement "int[][][] B = new int[7][5][11];".
It's possible to visualize the value of B as a solid 7-by-5-by-11 block of cells. Each cell holds an int and represents one position in the three-dimensional array. Individual positions in the array can be referred with variable names of the form B[i][j][k].
Ragu
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so ragu what are the toatl no ints that can be stored in such an array is it 7*5*11
------------------
coffee drinker
 
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic