This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Confusion about array instance  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Which two create an instance of an array? (Choose two)



Answer: A, D

How option D is creating an instance of Array.Please Explain.

Thanks,
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is an object. So a reference to an array can be assigned to a variable of type Object.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by yogi maheshnath:
Hi,

Which two create an instance of an array? (Choose two)



Answer: A, D

How option D is creating an instance of Array.Please Explain.

Thanks,




A. int[] ia = new int[15];
B. float fa = new float[20];
C. char[] ca = �Some String�;
D. Object oa = new float[20];
E. int ia[][] = { 4, 5, 6, }, { 1, 2, 3 };

I understand the following from the above choices

A. int[] ia = new int[15] -> This is correct

B. float fa = new float[20] -> This is incorrect, missing box brackets, the correct definition is
float[] fa = new float[20],

C. char[] ca = "Some String"

Please explain

D. Object oa = new float[20];

Please explain

E. int ia[][] = { 4, 5, 6, }, { 1, 2, 3 }; - > It should be declared like this ...
int[][] multiD = {
{ 1, 2, 3, 4 },
{ 5, 6, 7 }
};


Thanks
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char[] ca = "Some String"


"Some string" is a String and not an array of Characters.
It should be represented by

char[] ca={ 's','o','m','e','s','t','r','i','n','g','s'}.


And since array is an object, then you can assign it to the variable of type Object.
I dont know how to further explain this.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
char[] ca = "Some String"


"Some string" is a String and not an array of Characters.
It should be represented by

char[] ca={ 's','o','m','e','s','t','r','i','n','g','s'}.


And since array is an object, then you can assign it to the variable of type Object.
I dont know how to further explain this.



Thanks for your eply. explanation is good enough.

Some things needs to be memorized here..
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!