在Java中为链中的变量分配值 [英] Assigning a value to a variable in a chain in Java

查看:65
本文介绍了在Java中为链中的变量分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如果我们在像下面这样的链中给变量赋值,

If we assign a variable a value in a chain like the following,

int x=10, y=15;
int z=x=y;

System.out.println(x+" : "+y+" : "+z);

然后将所有三个变量的值 x y z 变为 15

then the value of all three variables x, y and z becomes 15.

但是我不了解数组的以下现象。

I however don't understand the following phenomenon with an array.

int array[]={10, 20, 30, 40, 50};
int i = 4;

array[i] = i = 0;    
System.out.println(array[0]+" : "+array[1]+" : "+array[2]+" : "+array[3]+" : "+array[4]);

输出 10:20:30:40:0 。它用 0 替换最后一个元素 array [4] 的值。

It outputs 10 : 20 : 30 : 40 : 0. It replaces the value of the last element which is array[4] with 0.

关于先前的赋值语句- int z = x = y; ,我希望第一个元素的值表示 array [0] 替换为 0 。为什么不是这样?很简单,但我不知道。

Regarding previous assignment statement - int z=x=y;, I expect the value of the first element means array[0] to be replaced with 0. Why is it not the case? It's simple but I can't figure it out. Could you please explain?

顺便说一下,该赋值语句 array [i] = i = 0; 是伪造的,在此代码中没有自己的价值,因此不再使用,但我只是想知道在这种情况下事情是如何工作的。

By the way, this assignment statement array[i] = i = 0; is dummy and it has no value of its own in this code and should no longer be used but I just wanted to know how thing actually works in this case.

推荐答案

int i = 4; 当我等于4时, array [ i] 等于 array [4] ,因此 array [i] = i = 0; 等效于 array [4] = i = 0; 。这样便可以将索引4的值更改为0。

int i = 4; when i equals to 4 the array[i] equals to array[4] so array[i] = i = 0; is equivalent to array[4] = i = 0;. That is way it change the value of index 4 with 0.

这篇关于在Java中为链中的变量分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆