Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can you explain this with code...

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the choices are working and what are the conditions to be followed while assigning arrays to other.

public class test{
public static void main(String [] args){

byte [][] big = new byte[7][7];
byte [][] b = new byte[2][1];
byte b3=5;
byte b2 [][][][] = new byte [2][3][1][2];

//Insert here
}
}

Which of the following can be inserted at the comment line(//Insert here) and still allow the code to compile.

1. b2[0][1] = b;
2. b[0][0] = b3;
3. b2[1][1][0] = b[0][0];
4. b2[1][2][0] = b;
5. b2[0][1][0][0] = b[0][0];
6. b2[0][1] = big;

Thanks...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Supriya Nimakuri:
How the choices are working and what are the conditions to be followed while assigning arrays to other.

public class test{
public static void main(String [] args){

byte [][] big = new byte[7][7];
byte [][] b = new byte[2][1];
byte b3=5;
byte b2 [][][][] = new byte [2][3][1][2];

//Insert here
}
}

Which of the following can be inserted at the comment line(//Insert here) and still allow the code to compile.

1. b2[0][1] = b;
2. b[0][0] = b3;
3. b2[1][1][0] = b[0][0];
4. b2[1][2][0] = b;
5. b2[0][1][0][0] = b[0][0];
6. b2[0][1] = big;

Thanks...



You can answer this question by considering the dimensions of the arrays.

big - 2
b - 2
b3 - 0
b2 - 4
b2[][] - 2
b[][] - 0
b2[][][] - 1
b2[][][][] - 0
[ June 28, 2006: Message edited by: Keith Lynn ]
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Keith ...I didnt get this logic...
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith:
I tried to run this program. It's running fine for every other options except option 3. Whereas I guess it should also compile...isn't it??

b2[1][1][0] = b[0][0];

Here, in the LHS b2 needs a single dimension object(i.e only a single variable) and on the RHS the b[0][0] point to a single variable. Hence it should be compatible with each other. I am not getting why it's giving error. Please help!!!


Thanks
Swapnil
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, b2[][][] is a 1 D array.
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnaks Keith!!!


Regards
Swapnil
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Keith or Swapnil...

Can you please explaim me the logic that u r applying to this question..

Regards
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. b2[0][1] = b;
2. b[0][0] = b3;
3. b2[1][1][0] = b[0][0];
4. b2[1][2][0] = b;
5. b2[0][1][0][0] = b[0][0];
6. b2[0][1] = big;

Hi Supriya, I am not too good at multi dimensional arrays but I will try to explain it... I will take it one by one:

1.b2 is a multidimensional array but here it's given as b2[0][1] hence, this will become a 2 D array(as 2 dimensions of b2 are already specified so there remains only 2). Whereas b is also a 2D array hence it will get fit into b2

2. I think this one is easy..here b[0][0] is the index position of element at (0,0) in b hence our primitive variable b3 will easily fit into it.

3. As I explained earlier b2[1][1][0] is a 3D array so remains only 1D Hence, it needs a 1D array and not a 2d. that's y b[0][0] can't fit into it.

4. Again we are trying to fit a 2D array into 1D array. Hence it"ll give error.

5.it's pretty clear.

6.here, b2 is also 2D array and big is also 2D array so b3 can be assigned to b2.

I think it helps...I tried my level best but still if u find any errors. I apologize for that.


Regards
Swapnil
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A similar question is posted on this thread...

array -please explain the output

Regards

Naseem
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Swapnil...
Thanx for explaining me...

Alos, Thans to Nassem and Keith
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is one way of seeing which declarations match.

reply
    Bookmark Topic Watch Topic
  • New Topic