java 数组索引和增量在同一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7218249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:08:39  来源:igfitidea点击:

array index and increment at the same line

java

提问by godMode

quick basic question, how does android/java handle the following lines of code:

快速基本问题,android/java 如何处理以下代码行:

int[] someArray = new int[5];
int index = 0;
int result;

result = someArray[index++];

which index would be passed on to the result? will it increment the index first then pass it to someArray[1]? or will it pass the original value of index to someArray[0] and then increment the index?

哪个索引会传递给结果?它会先增加索引然后将它传递给 someArray[1] 吗?还是将索引的原始值传递给 someArray[0] 然后增加索引?

回答by andronikus

From http://download.oracle.com/javase/tutorial/java/nutsandbolts/op1.html:

http://download.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value.

代码结果++;和++结果;都将以结果加一而告终。唯一的区别是前缀版本 (++result) 计算为递增值,而后缀版本 (result++) 计算为原始值。

So you'll get someArray[0].

所以你会得到someArray[0].

回答by derekerdmann

In Java and similar languages, using index++only increment the value after the expression has been evaluated. If you want to increment before using the variable, use ++index.

在 Java 和类似语言中,index++仅在表达式计算后使用递增值。如果您想在使用变量之前递增,请使用++index.

In this case, it will use the original value of indexto obtain result, and then increase its value to 1.

在这种情况下,它将使用 的原始值index来获取result,然后将其值增加到 1。

回答by Gravity

index++returns indexand then increments by 1. So it will do result = someArray[0]and then set indexto 1.

index++返回index然后增加 1。所以它会做result = someArray[0]然后设置index为 1。

In contrast, ++indexwould do the increment and then pass the incremented value. So if you wanted resultset to someArray[1]in the above code, you would use ++index.

相反,++index会做增量然后传递增量值。所以如果你想在上面的代码中result设置为someArray[1],你可以使用++index.

As someone else said, please don't use this kind of syntax. Instead, please write

正如其他人所说,请不要使用这种语法。相反,请写

index++;
result = someArray[index];

回答by Soufiane Hassou

It will pass someArray[0]and then increment index

它将通过someArray[0]然后递增index

It is not dependent from android, the general rule is:

它不依赖于android,一般规则是:

index++ means evaluates index and then increment it, while ++index is increment then evaluate

index++ 表示对 index 求值然后递增,而 ++index 是递增然后求值

回答by Brandon Langley

1.) Avoid doing this sort of thing, in fact with code I review I ask people to never do this.

1.) 避免做这种事情,事实上,在我的代码中,我要求人们永远不要这样做。

Not specifically using ++, but the fact you're doing it as part of evaluating something else. Generally it won't cost the compiler any extra to have that increment as a separate statement, and putting it inline like that means the next person coming along has to take a second and evaluate the increment themselves.

不是专门使用 ++,而是将它作为评估其他东西的一部分的事实。一般来说,将增量作为单独的语句不会花费编译器任何额外的费用,并且像这样将其内联意味着下一个人必须花一点时间自己评估增量。

I know it's minor, and it's a little nitpicky, but stuff like this costs extra time during code review, it's easy to miss while scanning, etc. And it saves you nothing but a few extra keystrokes, which when compared against code clairity and readability is not worth it IMO.

我知道这很次要,而且有点挑剔,但是像这样的东西在代码期间会花费额外的时间,在扫描时很容易错过等等。而且它只会为您节省一些额外的按键,与代码的清晰度和可读性相比不值得 IMO。

2) You will get someArray[0], and after moving on to the next line, you will have your index incremented.

2) 您将获得 someArray[0],在移至下一行后,您的索引将增加。