• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Once more about Array

 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one rule:
"If an array is declared as 2D array,you cant assign a 1D array to a 1D refrence."
Can someone explain me this with example.
Thanks.
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain your question in detail?
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did U mean,

If an array is declared as 2D array,you cant assign a 1D array to a 2D reference."


int[][] a; // declaration of a 2D Array.

int[] b = new int[10]; // 1D array declaration and Construction.

Now according to the rule U said above, You cannot do the below.

a = b;


Thanks.
[ February 04, 2008: Message edited by: Arjun Reddy ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry got reposted. My Bad..
[ February 04, 2008: Message edited by: Arjun Reddy ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry got reposted. My Bad..
[ February 04, 2008: Message edited by: Arjun Reddy ]
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is quite simple concept..
When you create on Object, three ways

1) either assign the reference variable equal to NULL
String[] str = null;

2) assign it to an Object on the heap without initialization



3) Declare and initialize at once



So now consider 2D array, What is 2D array

int[][] i = new int[5][];

here i refer to one Object on the heap and element of that array can refer to other Object,but currently they are not refering to any.

Similar to our second case...
str is referring to one object on the heap and element of that array can refer to other Object but currently they are not referring to any Object.
 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help.
Actually the question is:-
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];
//
}}
Which of the code could be inserted at "//" which will still allow the code to compile.
options:-
a) b2[0][1]=b;
b) b[0][0]=b3;
c) b2[1][1][0]=b[0][0];
d) b2[1][2][0]=b;
e) b2[0][1][0][0]=b[0][0];
f) b2[0][1]=big;
ans:-a,b,e,f
In the explanation of the ans I found this rule,which I have asked initially for.
Please explain how the assignment is going on.
Thanks
Sandhya
 
Henry Wong
author
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the explanation of the ans I found this rule,which I have asked initially for.
Please explain how the assignment is going on.



I have no idea what rule you are referring to. In assignments, the rule (for types) is simple, if the right side can be treated as the type of object as the reference type of the left side, the assignment is allowed.

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

b is a 2D array (or more specficially an array of array of byte). b2 is a 4D array. When b2 is dereferenced twice, the left side an 2D element reference. Same as the right side.

b) b[0][0]=b3;

b3 is a byte. b is a 2D array. When b is derefereced twice, the left side is a byte. Same as the right side.

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

b is a 2D array. When b is derefereced twice, the right side is a byte. b2 is a 4D array. When b2 is dereferenced thrice, the left side is a byte array reference element. Not the same as the right side.

d) b2[1][2][0]=b;

b is a 2D array. b2 is a 4D array. When b2 is dereferenced thrice, the left side is a byte array reference element. Not the same as the right side.

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

b is a 2D array. When b is derefereced twice, the right side is a byte. b2 is a 4D array. When b2 is dereferenced 4 times, the left side is a byte. Same as the right side.

f) b2[0][1]=big;

big is a 2D array. b2 is a 4D array. When b2 is dereferenced twice, the left side an 2D element reference. Same as the right side.


Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic