简体   繁体   English

赋值语句左侧的Java后缀增量运算符的语义是什么,为什么它们与右侧不同?

[英]What are the semantics of java's postfix increment operator on the left side of an assignment statement and why do they differ from the right side?

I was working on a leetcode question [1] and noticed something that caught my eye. 我工作在本文给出了问题[1],并发现了一些吸引我眼球。

When I write a line of code as: 当我编写一行代码时:

nums[i] = nums[(i++)+count];

I pass all the tests and my answer is accepted. 我通过了所有测试,答案被接受。 If however I change the same line to: 但是,如果我将同一行更改为:

nums[i++] = nums[i+count];

I get an ArrayOutOfBounds exception. 我收到了ArrayOutOfBounds异常。 Why is this? 为什么是这样?

Here is the full code: 这是完整的代码:

public void moveZeroes(int[] nums) {
    int count = 0, i = 0;
    while(i+count < nums.length){
        if(nums[i+count] == 0)
            count++;
        else
            nums[i] = nums[(i++)+count];
    }
    for(;i<nums.length; i++)
        nums[i] = 0;
}

[1] https://leetcode.com/problems/move-zeroes/ [1] https://leetcode.com/problems/move-zeroes/

The answer lies in JLS 15.26.1 . 答案在于JLS 15.26.1 Basically, this is the order of things in the case of 基本上,这是

nums[i++] = nums[i+count];
  • nums is evaluated (as part of the left-hand side) nums被评估(作为左侧的一部分)
  • i++ is evaluated to find the index; i++进行评估以找到索引; the result is the original value of i , but i is then incremented 结果是i的原始值,但随后i递增
  • At this point, we've evaluated which array element we're assigning to 至此,我们已经评估了要分配给哪个数组元素
  • nums is evaluated (as part of the right-hand side) nums被评估(作为右侧的一部分)
  • i+count is evaluated; i+count被评估; note that this uses the already incremented value of i , which is why you're getting an exception 请注意,这使用了已经递增i值,这就是为什么您遇到异常的原因
  • We now know which array element we're fetching 现在我们知道要获取哪个数组元素
  • Fetch the value of that element 获取该元素的值
  • Assign the value into the LHS element we identified earlier 将值分配给我们之前确定的LHS元素

The important part is that the left operand of the assignment operator is evaluated before the expression on the right operand. 重要的是,赋值运算符的左操作数在右操作数上的表达式之前进行求值。 So the side-effect while evaluating the array index expression in the left operand affects the evaluation of the right operand. 因此,在评估左侧操作数中的数组索引表达式时的副作用会影响右侧操作数的评估。

In the first example, i is being incremented after it is done being used in that iteration. 在第一个示例中,在迭代中使用完i后,将对其进行递增。

nums[(i++)+count];

means increment i by one after evaluating the equivalent of nums[(i) + count] . 表示在评估nums[(i) + count]的等效项后, i递增1。

In the second example, i is being incremented before nums[ i +count]; 在第二个示例中, inums[ i +count];之前递增nums[ i +count]; .

nums[i++]

means increment i by one after evaluating the equivalent of nums[i] . 表示在评估nums[i]的等效项后, i递增1。

When the code gets to nums[ i + count]; 当代码达到nums[ i + count]; , i has already been incremented. i已经增加了。 This is results in an ArrayOutOfBounds exception in the case that i + count is equal to nums.length . i + count等于nums.length的情况下,这将导致ArrayOutOfBounds异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Java中的后缀运算符从右到左求值? - Why do postfix operators in Java get evaluated from right to left? 为什么赋值的左侧不能是增量表达式? - Why can't the left hand side of an assignment be an increment expression? 为什么在Java 7中右手边没有漏掉Diamond运算符? - Why Diamond operator was not missed from Right hand side in Java 7? 可以在赋值运算符的左侧删除自动完成吗? - Can eclipse autocomplete on the left side of assignment operator? Java的逻辑OR运算符不评估右侧,尽管右侧具有一元运算符? - Java's logical OR operator does not evaluate right hand side despite that right hand side has a unary operator? Java Postfix增量运算符行为 - Java Postfix Increment Operator Behavior 在Java类DecimalFormat中,小数点左侧的主题标签有什么作用? - In the Java class DecimalFormat, what do the hashtags at the left side of the decimal do? JavaFX8如何从左侧而不是从右侧设置标签的省略号? - JavaFX8 How to set a label's ellipsis from the left-side not from the right-side? 为什么每个的左侧尺寸不等于右侧? - Why the left side size in for each is not equal to the right side? 为什么会出现“分配的左侧必须是变量”? - Why do I get “The left-hand side of an assignment must be a variable”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM