I got this question from a previous exam paper. I am trying to understand how do you arrive to the answer: 518
Consider the following Java program, consisting of the class Pins. Notice this is defined to have one constructor and one method named run. Both have no parameters.
class Pins {
private int[][] ints = {{0,1,2}, {3,4,5},{6,7,8}};
public Pins()
{
int[] a = ints[0];
ints[0][2] = ints[0][1];
ints[0] = ints[1];
ints[1] = a;
}
public void run()
{
int i = -1;
while (++i < ints.length)
{
int total = 1;
for (int j = 0; j < ints.length; j++) {
if (j % 2 == 0) {
total += ints[i][j];
} else {
total -= ints[i][j];
}
}
System.out.println(total);
}
}
}
Given an instance of this class, what is printed out by its run method? Use diagrams to justify and explain your answer.
>Why does a become {0,1,1} after the second step in the constructor and not {3,4,5} in the third step in the constructor?
The constructor executes like so:
int[] a = ints[0]; // ints: {a: {0,1,2}, {3,4,5}, {6,7,8}}
ints[0][2] = ints[0][1]; // ints: {a: {0,1,1}, {3,4,5}, {6,7,8}}
ints[0] = ints[1]; // ints: {{3,4,5}, {3,4,5}, {6,7,8}}; a: {0,1,1}
// \-same-array/
ints[1] = a; // ints: {{3,4,5}, {0,1,1}, {6,7,8}}
And then in run()
, what it does, is, for every row, it computes:
1 + row[0] - row[1] + row[2];
So:
1 + 3 - 4 + 5 = 5
1 + 0 - 1 + 1 = 1
1 + 6 - 7 + 8 = 8
What's potentially tricky is that ints
contains references to the rows, so when you say a = ints[0]
, a
points to the row, rather than containing a copy of it.
You see it better, if you write down the single results (just some System.out.println(...); added):
class Pins
{
private int[][] ints = {{0,1,2}, {3,4,5},{6,7,8}};
public Pins()
{
int[] a = ints[0];
ints[0][2] = ints[0][1];
ints[0] = ints[1];
ints[1] = a;
}
public void run()
{
for(int i=0; i < ints.length; i++){
for(int j=0; j < ints[i].length;j++){
System.out.print(ints[i][j]+";");
}
System.out.println("");
}
int i = -1;
while (++i < ints.length)
{
int total = 1;
for (int j = 0; j < ints.length; j++)
{
if (j % 2 == 0)
{
total += ints[i][j];
} else
{
total -= ints[i][j];
}
System.out.println("Total:"+total);
}
System.out.println(total);
}
}
public static void main(String[] args){
Pins pin = new Pins();
pin.run();
}
}
results:
3;4;5; //ints[0] after swap
0;1;1; //ints[1] after swap
6;7;8; //ints[2] after swap
Total:4
Total:0
Total:5
5
Total:1
Total:0
Total:1
1
Total:7
Total:0
Total:8
8
Constructor changes ints[][] around:
int[] a = {0,1,2} //references ints[0]
ints[0][2] = ints[0][1] // ints[][] = {{0,1,1},{3,4,5},{6,7,8}}
// a = {0,1,1} since it references ints[0]
ints[0] = ints[1] // ints[][] = {{3,4,5},{3,4,5},{6,7,8}}
//changes reference of ints[0], a[] still pointing to old reference
ints[1] = a // ints[][] = {{3,4,5},{0,1,1},{6,7,8}
//points ints[1] back to a's reference
//final ints: {{3,4,5},{0,1,1},{6,7,8}
i starts as -1.
++i increments before comparing, so the first while check is 0 < ints.length (which is currently 3). i is now 0 total is 1
for j = 0; j < 3; j++
j = 0
if (j%2 == 0) //true
total += 3 //total = 4
j = 1
if (j%2 == 0) //false
total -= ints[0][1] //4, total = 0
j = 2
if (j%2 == 0) //true
total += ints[0][2] //5, total = 5
j = 3
Print "5"
i is now 1 total is 1
for j = 0; j < 3; j++
j = 0
if (j%2 == 0) //true
total += ints[1][0] //0, total = 1
j = 1
if (j%2 == 0) //false
total -= ints[1][1] //1, total = 0
j = 2
if (j%2 == 0) //true
total += ints[1][2] //1, total = 1
j = 3
Print "1"
i is now 2 total is 1
for j = 0; j < 3; j++
j = 0
if (j%2 == 0) //true
total += ints[2][0] //6, total = 7
j = 1
if (j%2 == 0) //false
total -= ints[2][1] //7, total = 0
j = 2
if (j%2 == 0) //true
total += ints[2][2] //8, total = 8
j = 3
Print "8"
Final output:
5
1
8
(Println will print on separate lines, so for "518" you'd need to just System.out.print())
after getting instance of pin this array look like:
{3,4,5}
{0,1,1}
{6,7,8}
when i = 0(traversing row 0) total = 1+3-4+5=5
when i = 1(traversing row 1) total = 1+0-1+1=1
when i = 2(traversing row 2) total = 1+6-7+8=8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.