• 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

Dan's Mock test on Mutil-dimensional arrays

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a question regarding Dan's exam multi-dimensional arrays:
class MWC212 {
public static void main(String[] args) {
int[] a1[],a2[]; // 1
int []a3,[]a4; // 2
int []a5,a6[]; // 3
int[] a7,a8[]; // 4
}}
Above code gives a compile time error at //2
because the square brackets appear before the identifier
for array variable a4 are not associated with the type or the identifier.
So if I change //2 to
int []a3, int[] a4; //2
still does not compile. Here I have placed a type ie int. The square brackets appear after the type.
Can anyone please let me know why is this so ?
Thanks,
Gayatri.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot club the declaration of one variable type with that of other without an explicit statement statement delimitter ";" in between.
Here, "," can be used only for sequential execution. Say, in a for loop, incrementing a set of counters or in a declaration of a set of variables of a single type.. e.t.c.
In the above piece of code, if you replace
int []a3, int[] a4;
with
int []a3; int[] a4;
or
int []a3, a4;
it will work fine.
Cheers,
Rama
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can also be:
int []a3, a4[];
Succeeding array variables should place [] after the variable.
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rama and Dennis.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gayatri, keep in mind that a4[] is a two dimensional array which is different from Rama's example. Rama's example is just a one-dimensional array. Based on your code above, I'm assuming you are creating a2[], a4[], a6[], and a8[] as two-dimensional arrays.
[ January 13, 2004: Message edited by: dennis zined ]
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic