Java 2D数组,了解输出

时间:2016-01-19 17:26:39

标签: java arrays

我从以前的试卷中得到了这个问题。我想知道你是如何得出答案的: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方法打印出什么?使用   图表证明并解释你的答案。

&gt;为什么a在构造函数的第二步之后成为{0,1,1}而在构造函数的第三步中不成为{3,4,5}?

4 个答案:

答案 0 :(得分:2)

构造函数执行如下:

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指向该行,而不是包含该行的副本。

enter image description here

答案 1 :(得分:0)

你看得更清楚,如果你写下单个结果(只是一些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

答案 2 :(得分:0)

构造函数更改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在比较之前递增,因此第一次检查时为0&lt; 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())

答案 3 :(得分:0)

获取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