简体   繁体   English

调用内的Java增量/减量运算符

[英]java increment/decrement operator inside call

We're learning about quickSort. 我们正在学习quickSort。 The book provides the code at the end of my question. 本书在我的问题结尾处提供了代码。

I'm curious about this call at the end of the findPivot method: 我对findPivot方法末尾的调用感到好奇:

swap(array, left++, right--);

Why have the "++" and "--" in there? 为什么在其中有“ ++”和“-”? It's not incrementing/decrementing either variable (before or after the swap) and it's not accessing (for example) array[left + 1]. 它不是在交换之前或之后递增/递减变量,也不在访问(例如)array [left + 1]。

So what gives? 那有什么呢?

EDIT 编辑

So before I posted the question I wrote the following test code: 因此,在发布问题之前,我编写了以下测试代码:

public static void main(String[] args) {

    int a = 0;
    int b = 2;
    int[] array = {1,10,20,30};

    swap(array, a++,b--);

}

public static void swap(int[]array,int a, int b) 
{
    for(int i = 0; i < 10; i++)
    {
    Integer temp = array[a];
   array[a] = array[b];
   array[b] = temp;

    System.out.println("a = " + a + "\nb = " + b + "\narray a: " + array[a] + "\narray b: " + array[b]);
    }

The results are as follows: 结果如下:

a = 0 b = 2 array a: 20 array b: 1 a = 0 b = 2 array a: 1 array b: 20 a = 0 b = 2 array a: 20 array b: 1 a = 0 b = 2数组a:20数组b:1 a = 0 b = 2数组a:1数组b:20 a = 0 b = 2数组a:20数组b:1

The variable isn't post-incrementing at all when used in the method. 在该方法中使用时,该变量根本不后递增。 That's why I asked the question. 这就是为什么我问这个问题。

Thanks. 谢谢。 Here's the code: 这是代码:

private static void swap(Integer[] array, int i, int j)
{

   Integer temp = array[i];
   array[i] = array[j];
   array[j] = temp;

}

public static void quickSort(Integer[] array, int left, int right)
{
    if(left < right)
    {
        int pivot = findPivot(array, left, right);
        quickSort(array, left, pivot - 1);
        quickSort(array, pivot + 1, right);
    }
}//quickSort

public static int findPivot(Integer[] array, int left, int right)
{
    int first = left++;
    while (left <= right)
    {
        while (left <= right && array[first].compareTo(array[left]) > 0)
        {
            left++;
        }
        while (left <= right && array[first].compareTo(array[right]) < 0)
        {
            right--;
        }
        if (left < right)
            swap(array, left++, right--);
    }
    swap(array, first, right);
    return right;
}

It are post-increment (§15.14.2) ( ++ ) and post-decrement (§15.14.3) ( -- ) operations. 它是后递增(§15.14.2)++ )和后递减(§15.14.3)-- )操作。 These will change the values for the next iteration in your while loop. 这些将更改while循环中下一次迭代的值。

You basically have this: 您基本上有这个:

while (left <= right)
{
    // ...
    if (left < right)
    {
        swap(array, left, right);
        left++;
        right--;
    }
}

As you can see, the "post" means that the values are not affected for that particular statement. 如您所见,“ post”表示该值不受该特定语句的影响。 After evaluating the post increment operation, it will the variable will be increased, but the value passed to the method was still the old one. 在评估后递增操作后,将增加该变量,但传递给该方法的值仍然是旧值。 For the advanced readers, you could write the post-increment operator like this (pseudo code): 对于高级读者,您可以这样编写后递增运算符(伪代码):

public int operator this++()
{
    int temp = this;
    ++this; // regular pre-increment (JLS §15.15.1)
    return temp;
}

For a read about the pre-increment operator, you can check JLS §15.15.1 . 要了解有关预递增运算符的信息,可以查看JLS§15.15.1

swap(array, left++, right--); is inside of a while loop, so the updated values will be used in the next loop iteration. while循环内,因此更新的值将在下一个循环迭代中使用。

java increment( ++ ) and decrement( -- ) lets suppose i++; Java的increment( ++ )和decrement( -- )让我们假设i++; this means i+1 and i-- means i-1 in programming 这意味着i+1i--表示编程中的i-1

int i = 1;
System.out.println("i : "+(i++)); // this means first print then add `1` into i;
//and
System.out.println("i : "+(++i)); // this means first add one and then print it;
// same for '--' 

They are the post increment (++) and post decrement (--) operation, so they will change the values in the next iteration in your while loop 它们是后递增(++)和后递减(-)操作,因此它们将在while循环的下一次迭代中更改值

In your code at this line 在此行的代码中

   swap(array, left++, right--);//left++ , right-- are post increment (++) and post decrement (--) operation

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM