aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Once more about Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Once more about Array" Watch "Once more about Array" New topic
Author

Once more about Array

sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
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

Joined: Dec 29, 2007
Posts: 129
Can you please explain your question in detail?
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
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 ]

Be Humble... Be Nice.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Sorry got reposted. My Bad..
[ February 04, 2008: Message edited by: Arjun Reddy ]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 629
Sorry got reposted. My Bad..
[ February 04, 2008: Message edited by: Arjun Reddy ]
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Once more about Array