• Post Reply Bookmark Topic Watch Topic
  • New Topic

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!