Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Reference Assignments

 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the basic rules to follow when assigning array references for multi-dimensional arrays? What I mean is: "What can I assign to an array reference variable? " Is there a tutorial for this?. I am getting the dimensions wrong with these types of questions.
Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give us a question that gets you confused. Maybe we can explain it.
Here's one that came up recently: A way of "seeing" a "three dimensional" array.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer is that you can assign to an array reference variable either null or a reference to any array with a compatible component (element) type and the same number of dimensions. In the case of an array of primitive type components, the array reference variable must have the identical component type.

For example, if "A a[][][];", "a" can hold a three dimensional array of components of reference type "X" if and only if "X instanceof A".

However, if "int i[];" and "short s[] = {1, 2, 3};", "i = s;" will not compile.

The more interesting cases involve assignments of references to sub-arrays like one column of a two-dimensional array.
[ December 13, 2004: Message edited by: Mike Gershman ]
 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is from K&B's mock exam.

public class Test {
public static void main (String [] args) {
short[][] b = new short[4][4];
short[][] big = new short[2][2];
short b3 = 8;
short b2[][][] = new short[2][3][2][2];
//insert code here
}
}

A) b2[1][1] = big;
B) b[1][0] = b3;
C) b2[0][1][1] = b;
D) b2[0][2][1] = b[1][0];
E) b2[1][1][0][1] = b[1][0];
F) b2[1][1] = b;

Please explain the basic rules to keep in mind while attempting this question. I know it has to do with the array dimensions. Please explain in detail.
Thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best rule on this type of assignment is that the number of missing [] in the left-hand expression must match the number of missing [] in the right-hand expression.

By "number of missing []" I mean subtract the number of [] in the expression from the number of [] in the original declaration of that array reference variable.

Of course, the component types must also be compatible - see my post above.

By the way, "short b2[][][] = new short[2][3][2][2];" is invalid.
[ December 13, 2004: Message edited by: Mike Gershman ]
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mike

couldn't let this one pass by unnoticed:-


The best rule on this type of assignment is that the number of missing [] in the left-hand expression must match the number of missing [] in the right-hand expression.By "number of missing []" I mean subtract the number of [] in the expression from the number of [] in the original declaration of that array reference variable.


C. b2[1][1][0] = b[0][0]; // [2 -3 ]..?
D. b2[1][2][0] = b; // [0-3 ]..?
E. b2[0][1][0][0] = b[0][0];// [2-4]..?

could you pl. demo an example of this [] arithmetic with the given C,D and E
Thx.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing, Netty:

C. b2[1][1][0] = b[0][0]; // [2 -3 ]..? << 3-3 == 2-2 >> OK
D. b2[1][2][0] = b; // [0-3 ]..? << 3-3 != 2-0 >> Bad
E. b2[0][1][0][0] = b[0][0];// [2-4]..? << 3-4 != 2-2 >> Bad
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Q:-which of the following lines of code could be inserted at line 7, and still allow the code to
compile? (Choose four that would work.)
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;

Now Correct me if I am wrong here.

The answer given to this question is:

12. A, B, E, and F. This question covers the issue of, �What can I assign to an array reference
variable?� The key is to get the dimensions right. For example, if an array is declared as a
two-dimensional array, you can�t assign a one-dimensional array to a one-dimensional array
reference.
C is wrong because it tries to assign a primitive byte where a byte array (one dimension) is
expected. D is wrong because it tries to assign a two-dimensional array where a one-dimensional
array is expected.



while it was mentioned :
C. b2[1][1][0] = b[0][0]; // [2 -3 ]..? << 3-3 == 2-2 >> OK
D. b2[1][2][0] = b; // [0-3 ]..? << 3-3 != 2-0 >> Bad
E. b2[0][1][0][0] = b[0][0];// [2-4]..? << 3-4 != 2-2 >> Bad

[as per the book answer C and D are at fault and E is fine ]

if this is taking too much of an effort, let's forget this one,
"got bigger fish to fry"
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Sumithab's example:
short b2[][][] = new short[2][3][2][2];

In Netty's example:
byte b2 [][][][] = new byte [2][3][1][2];

THe extra [] does make a difference.
 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, I don't get it.

Let's take the C option. The arrays that are involved are b and b3.
Originally: b has 2 [] and b2 has 3 []. Right?
In the expression: b has 0 [] and b2 has 3 []. Right?
Taking b: original [] - expression [] = 2 - 0 = 2. Right?
Taking b2: original [] - expression [] = 3-3 = 0. Right?
Result 0 not equal to 2? Right???

Thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to repost the problem as stated by Sumithab and a different problem posted by Netty:

Sumithab:

public class Test {
public static void main (String [] args) {
short[][] b = new short[4][4];
short[][] big = new short[2][2];
short b3 = 8;
short b2[][][] = new short[2][3][2][2];
//insert code here
}
}

A) b2[1][1] = big;
B) b[1][0] = b3;
C) b2[0][1][1] = b;
D) b2[0][2][1] = b[1][0];
E) b2[1][1][0][1] = b[1][0];
F) b2[1][1] = b;



Netty:

1. public class Test {
2. public static void main(String [] args) {
3. byte [][] big = new byte [7][7];
4. byte [][] b = new byte [2][1];
5. byte b3 = 5;
6. byte b2 [][][][] = new byte [2][3][1][2];
7.
8. }
9. }


Q:-which of the following lines of code could be inserted at line 7, and still allow the code to
compile? (Choose four that would work.)
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;


Please note how the declaration for b2 has 3 [] in Sumithab's case and 4 [] in Netty's case. Please note how answers C and D in Sumithab's case became answers D and C in Netty's case.

Please pick one of these two cases and I will be happy to answer your questions.
 
Sumithab Baskaran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, please explain my question.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take the C option. The arrays that are involved are b and b3.
Originally: b has 2 [] and b2 has 3 []. Right?
In the expression: b has 0 [] and b2 has 3 []. Right?
Taking b: original [] - expression [] = 2 - 0 = 2. Right?
Taking b2: original [] - expression [] = 3-3 = 0. Right?
Result 0 not equal to 2? Right???


All correct.
 
lak naidu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike! It took a while for me to sink it in...very cool method to check the validity.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone care to explain--> "In the expression: b has 0 [] ..."
this one's eluding me
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone care to explain-->
Let's take the C option. The arrays that are involved are b and b3.
Originally: b has 2 [] and b2 has 3 []. Right?
In the expression: b has 0 [] and b2 has 3 []. Right?
Taking b: original [] - expression [] = 2 - 0 = 2. Right?
Taking b2: original [] - expression [] = 3-3 = 0. Right?
Result 0 not equal to 2? Right???


The best rule on this type of assignment is that the number of missing [] in the left-hand expression must match the number of missing [] in the right-hand expression.

By "number of missing []" I mean subtract the number of [] in the expression from the number of [] in the original declaration of that array reference variable.




Refer to question "C" in Sumithab's version of the problem:


"Originally: b has 2 [] and b2 has 3 []."

short[][] b = new short[4][4];
short b2[][][] = new short[2][3][2][2];

as you can see,
b is declared as short[][]
and b2 is declared as short[][][]

-----------------------------------------------------

"In the expression: b has 0 [] and b2 has 3 []."

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

as you can see,
b is used with 0 [] in the expression
and b2 is used with 3 [] in the expression

------------------------------------------------------

"Taking b: original [] - expression [] = 2 - 0 = 2.
Taking b2: original [] - expression [] = 3-3 = 0.
Result 0 not equal to 2"

Do you see it now?
[ December 17, 2004: Message edited by: Mike Gershman ]
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
!
finally got it....
I was on the fact that there's a subtle difference between: int[] i; and int i[];
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that int[] i; and int i[]; are equivalent.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
absolutely right, from that point of context.
But......the subtle difference is, if one declares multiple variables in the same statement such as: int[] i, j; and int i[], j;, then j is not of the same type.
In the first case, j is an array of integers while in the second case, j is just an integer.
[ December 18, 2004: Message edited by: Netty poestel ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic