[英]Java 2D arrays, understanding output
我從以前的試卷中得到了這個問題。 我試圖了解您如何得出答案:518
考慮下面的Java程序,該程序由Pins類組成。 請注意,這被定義為具有一個構造函數和一個名為run的方法。 兩者都沒有參數。
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);
}
}
}
給定此類的實例,其run方法將輸出什么? 使用圖表說明並解釋您的答案。
>為什么a在構造函數的第二步之后變成{0,1,1}而不在構造函數的第三步變成{3,4,5}?
構造函數執行如下:
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}}
然后在run()
,它對每一行進行計算:
1 + row[0] - row[1] + row[2];
所以:
1 + 3 - 4 + 5 = 5
1 + 0 - 1 + 1 = 1
1 + 6 - 7 + 8 = 8
什么是潛在的麻煩是ints
包含對行的引用,所以當你說a = ints[0]
a
指向該行,而不是包含它的一個副本。
如果寫下單個結果(只是添加了一些System.out.println(...);),您會看到更好的結果:
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();
}
}
結果:
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
構造函數將ints [] []更改為:
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}
我從-1開始。
++ i在比較之前會增加,因此,第一個while校驗為0 <ints.length(當前為3)。 我現在是0總數是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"
我現在是1總數是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"
我現在是2總數是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"
最終輸出:
5
1個
8
(Println將在單獨的行上打印,因此對於“ 518”,您只需要System.out.print())
獲得pin實例后,該數組如下所示:
{3,4,5}
{0,1,1}
{6,7,8}
當i = 0(遍歷第0行)總計= 1 + 3-4 + 5 = 5
當i = 1(遍歷第1行)時總計= 1 + 0-1 + 1 = 1
當i = 2(遍歷第2行)總計= 1 + 6-7 + 8 = 8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.