Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mike Gershman
SCJP 1.4, SCWCD in process
Mike Gershman
SCJP 1.4, SCWCD in process
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.
Mike Gershman
SCJP 1.4, SCWCD in process
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.
Mike Gershman
SCJP 1.4, SCWCD in process
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;
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;
Mike Gershman
SCJP 1.4, SCWCD in process
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???
Mike Gershman
SCJP 1.4, SCWCD in process
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.
Mike Gershman
SCJP 1.4, SCWCD in process
Mike Gershman
SCJP 1.4, SCWCD in process
I will suppress my every urge. But not this shameless plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|