• 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

Doubt from Cathy's book

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How many objects will be created by the following code.
Cat[][] mycats = {{new Cat(),new Cat()},
{new Cat(),new Cat(),new Cat()}};
I have taken this from Cathy's book for SCJP guide.The answer says 8 objects but i thought six objects would be created.Though there is a nice pictorial representation of the whole process,am still not getting about the two extra objects in the answer.Please point me where am going wrong??

Let us make the world a better place to live

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramnath -
Here's a hint... in Java what does a multi-dimensional array REALLY mean? Is a multidimensional array really just one array?
-Bert
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mycats[0] is an array object that is holding two Cats objects. The first Cat in that array is mycats[0][0], and the last one is mycats[0][1].
Similarly, mycats[1] is the second (missing) array object that holds three Cats objects. The second Cat object in this array is pointed to by mycats[1][1]
mycats is a variable that points to an array object that holds two objects, they are mycats[0] and mycats[1] array objects!
 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow,
Thanks Bert and Jose.I got it now.Thanks for ur help and guess what!!! am getting all the questions correct on arrays in the practice exam now.
Thanks,
Ramnath
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So totally should be 5 Cat objects. Right?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5 cats are created.
3 array objects are created.
 
reply
    Bookmark Topic Watch Topic
  • New Topic