Java 2D数组,了解输出
我从以前的试卷上得到了这个问题。我试图理解你是如何得出答案的:518 考虑下面的Java程序,它由类pin组成。注意,它定义为有一个构造函数和一个名为run的方法。两者都没有参数Java 2D数组,了解输出,java,arrays,Java,Arrays,我从以前的试卷上得到了这个问题。我试图理解你是如何得出答案的:518 考虑下面的Java程序,它由类pin组成。注意,它定义为有一个构造函数和一个名为run的方法。两者都没有参数 class引脚{ 私有int[][]int={{0,1,2},{3,4,5},{6,7,8}; 公共PIN() { int[]a=int[0]; 整数[0][2]=整数[0][1]; 整数[0]=整数[1]; ints[1]=a; } 公开募捐 { int i=-1; 而(++i
class引脚{
私有int[][]int={{0,1,2},{3,4,5},{6,7,8};
公共PIN()
{
int[]a=int[0];
整数[0][2]=整数[0][1];
整数[0]=整数[1];
ints[1]=a;
}
公开募捐
{
int i=-1;
而(++i
给定这个类的一个实例,它的run方法输出了什么?使用
用图表来证明和解释你的答案
为什么在构造函数的第二步之后,a变成了{0,1,1},而不是在构造函数的第三步之后变成了{3,4,5}?如果你写下单个结果(只是一些System.out.println(…);添加),你会看到更好的结果:
构造函数的执行方式如下:
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];
因此:
潜在的棘手问题是ints
包含对行的引用,因此当您说a=ints[0]
时,a
指向行,而不是包含行的副本
构造函数围绕以下内容更改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
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"
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"
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"
j=0时的;j<3;j++
j=0
如果(j%2==0)//true
总计+=3//总计=4
j=1
如果(j%2==0)//false
总计-=整数[0][1]//4,总计=0
j=2
如果(j%2==0)//true
总计+=整数[0][2]//5,总计=5
j=3
打印“5”
我现在1岁了
总数是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"
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"
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"
j=0时的;j<3;j++
j=0
如果(j%2==0)//true
总计+=整数[1][0]//0,总计=1
j=1
如果(j%2==0)//false
总计-=整数[1][1]//1,总计=0
j=2
如果(j%2==0)//true
总计+=整数[1][2]//1,总计=1
j=3
打印“1”
我现在2岁了
总数是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"
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"
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"
j=0时的;j<3;j++
j=0
如果(j%2==0)//true
总计+=整数[2][0]//6,总计=7
j=1
如果(j%2==0)//false
总计-=整数[2][1]//7,总计=0
j=2
如果(j%2==0)//true
总计+=整数[2][2]//8,总计=8
j=3
打印“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行)total=1+6-7+8=8时,您需要手动“运行”程序,跟踪数组中的内容以及每个步骤中的不同变量。唯一棘手的一点是它们在数组中交换行的位置。我这样做了,我通过运行程序得到518,手工得到528。交换数组位是我陷入困境的一点,为什么a在构造函数的第二步之后变成{0,1,1},而不是在构造函数的第三步中变成{3,4,5}?
a
总是指向{0,1,2或1}
array,它仅在第一步中设置,之后不会修改ints[0]…ints[2]
和a
都是对内存中其他地方3个元素的数组的引用。为什么在构造函数的第二步之后a变成了{0,1,1},而在构造函数的第三步之后不是{3,4,5}?Vlad回答得非常好。“a”是对“ints[0];”值的引用,此时{0;1;1}
。区别在于引用变量和值变量!在第2行(ints[0][2]=…)中,您更改的是值,而不是引用,在第3行和第4行中,您只需交换引用。你可以想象这就像文件和指向文件的链接,引用变量的行为就像链接,值变量的行为就像文件。只有简单变量是值变量。所有其他的(如数组)都是引用变量。为什么A在第二次执行时不改变int[0][2]
=int[0][1]
<代码>公共Pins(){int[]a=ints[0];ints[0][2]=ints[0][1];ints[0]=ints[1];ints[0][2]=ints[0][1];ints[1]=a;}In ints[0]=ints[1];您只需更改ints[0]的引用,而不是A引用的“文件”的值。意思是ints[0]现在与ints[1]是同一个链接。“文件”A指的是保持原样。在Vlads图中,这意味着更改箭头,而不是内容。