• 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

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
taken from: http://www.jdiscuss.com

Which of the following code fragments will successfully initialize a two-dimensional array of chars named cA with a size such that cA[2][3] refers to a valid element?

1.
char[][] cA = { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };

2.
char cA[][] = new char[3][];
for (int i=0; i<cA.length; i++) cA[i] = new char[4];

3.
char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{ 'a', 'b', 'c' } };

4
char cA[3][2] = new char[][] { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };

5.
char[][] cA = { "1234", "1234", "1234" };

my answer is 1 & 3 but the answer given is 2. i am a little confused
please anyone can help me

thanks in advance
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The question is about the declaration of 2-dim array cA such that cA[2][3] should be a valid element. That means the array size should be atleast [3][4].

1.
char[][] cA = { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };
//This will compile and creates array of size [2][3]. As cA[2][3] will give ArrayIndexOutOfBounds Exception, this is not the answer.
2.
char cA[][] = new char[3][];
for (int i=0; i<cA.length; i++) cA[i] = new char[4];
//This will compile and creates array of size [3][4]. As cA[2][3] is accessible, this is one of the answers.
3.
char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{ 'a', 'b', 'c' } };
//This will compile and creates array of size [2][3]. As cA[2][3] will give ArrayIndexOutOfBounds Exception, this is not the answer.
4
char cA[3][2] = new char[][] { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };
//This wont compile because the size is mentioned in the declaration. When the array is declared and initialized in the same line, size should not be mentioned!

5.
char[][] cA = { "1234", "1234", "1234" };
// This also wont compile because the "1234" is a string and cannot be converted to char[].

So the answer is 2.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srilatha The question is about the declaration of 2-dim array cA such that cA[2][3] should be a valid element. That means the array size should be atleast [3][4].

Why did you said so can you please explain
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi M SRILATHA

The question is about the declaration of 2-dim array cA such that cA[2][3] should be a valid element. That means the array size should be atleast [3][4].

this is what i am unable to understand. please explain.

thanks
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Srilatha meant this...
char[][] cA = { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };
char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{ 'a', 'b', 'c' }

Both of them initialize a 2d char array of size[2][3] with the following values:
[0][0]='a',[0][1]='b',[0][2]='c'
[1][0]='a',[1][1]='b',[1][2]='c'

so in these arrays ,when you try to access element [2][3] ,exception is thrown.so not possible answers.
Now for element[2][3] to be accessed,the array size should be greater than that ,since the index starts at 0 which option 2 does.

char cA[][] = new char[3][];
for (int i=0; i<cA.length; i++) cA[i] = new char[4];

It initializes a 2d array of size[3][4] .Now accessing [2][3] is valid.
I am not sure if my explanation is of any help!
Sure somebody will correct me if Iam wrong...
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in both 1 and 3,there will be an ArrayIndexOutOfBounds Exception as [2][3] will be an illegal index value....
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The question is about the declaration of 2-dim array cA such that cA[2][3] should be a valid element. That means the array size should be atleast [3][4].



Index for Arrays will be from 0 to Array.length-1.
if cA[2][3] has to refer to a valid element, the array should be of size atleast [3][4] ( [2+1][3+1] ). Why i said atleast is because the array can be of size [4][5] also where cA[2][3] is a valid element.

I hope this is clear!
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks M SRILATHA for giving detail expalanation
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic