java - 为什么for循环可以初始化Java中按值传递的变量

标签 java oop for-loop reference

代码:

 import java.util.Arrays;

class Heapify {
  int[] array;

  Heapify (int[] array){
   this.array = array;
  }


  public  void heap (int[] aArray){
    int left = 0;
    int right  = aArray.length - 1;
    int n = aArray.length;
    int numOfSwap  = 0;    
    heapify1(aArray, left, right, numOfSwap );  
  }  


  public void heapify1 (int[] aArray, int left, int right, int numOfSwap) {
    //from last one that have a child
    for (int i = (right - 1)/2; i > left - 1;  --i){  
      System.out.println("in heapify for loop with i =  " + i + "    number of swap: " + numOfSwap);
      if (aArray[2*i + 2] > aArray[i]) {//if right child is bigger than parent
        System.out.println("in heapify for loop and if #1: " + numOfSwap);
        swap(aArray, i, 2*i + 2, right, numOfSwap);
      }
      else if (aArray[2*i + 1] > aArray[i]){
        System.out.println("in heapify for loop and else if #2: " + numOfSwap);
        swap(aArray, i, 2*i + 1, right, numOfSwap);
      }
    }
  }


  public int swap (int[] aArray, int parent, int child, int right, int numOfSwap ){
    System.out.println("in swap before: " + numOfSwap );
    int temp = aArray[parent];
    aArray[parent] = aArray[child];
    aArray[child] = temp;
    numOfSwap  = numOfSwap  + 1;
    System.out.println("inswap after: " + numOfSwap );
    heapify1 (aArray, parent, right, numOfSwap );
    return numOfSwap;
  }


  public static void main(String args[]) {
    int[] array1 = new int[] {21,15,25,3,5,12,7,45,19,2,9};

    Heapify hs = new Heapify(array1);
    hs.heap(array1);
  }
}

结果打印值为:

in heapify: 0
in heapify for loop with i = 4    number of swap: 0
in heapify for loop and if #1: 0
in swap before: 0
inswap after: 1
in heapify: 1
in heapify for loop with i = 4    number of swap: 1
in heapify for loop with i = 3    number of swap: 0
in heapify for loop and if #1: 0
in swap before: 0
inswap after: 1
in heapify: 1
in heapify for loop with i = 4    number of swap: 1
in heapify for loop with i = 3    number of swap: 1
in heapify for loop and else if #2: 1
in swap before: 1
inswap after: 2
in heapify: 2
in heapify for loop with i = 4    number of swap: 2
in heapify for loop with i = 3    number of swap: 2
in heapify for loop with i = 2    number of swap: 0
in heapify for loop with i = 1    number of swap: 0
in heapify for loop and else if #2: 0

我不明白为什么swapnumOfSwap = 1传递给heapify1,但是在for循环中,第二次迭代,numOfSwap 被值 0 覆盖。另外,为什么在后面的情况下,这种覆盖行为发生在第 3 次迭代时?

我知道这可能是“按引用传递”或“按值传递”的问题,但是真正让我困惑的一件事是 numOfSwap 在swap,swap 再次调用 heapify1,成功将 numOfSwap = 1 传递给 heapify1。但为什么 numOfSwap 在迭代后会在 for 循环中初始化呢?

最佳答案

heapify1 方法有两种不同的上下文。

其中之一是在 heap 方法中调用 heapify1(aArray, left, right, numOfSwap ); 时。在此上下文中,numOfSwap 为 0,并且保持为 0。在该上下文中,执行 for 循环。它可能看起来像“迭代后 numOfSwap 在 for 循环中初始化?”,但事实并非如此。该值永远不会被修改并保持为 0。

另一个上下文是在 swap 方法中调用 heapify1 (aArray,parent, right, numOfSwap ); 时,也就是说“swap 再次调用 heapify1 并传递 numOfSwap = 1成功heapify1”。但这些传递的值仅在此上下文中使用,与提到的第一个上下文无关,并且不会修改 numOfSwap 的值(0)。

关于java - 为什么for循环可以初始化Java中按值传递的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50072094/

相关文章:

variables - 使用 Xpath for..in..return 时在 XSLT/XPath 中创建增量计数变量?

javascript - 如何(中断;同时)和(继续;循环)

java - short 的性能/内存优势是否因向下转换而无效?

java - 通过更改替换数组的元素

java - pig : Hadoop jobs Fail

c# - 向按钮控件添加自定义属性 C#

oop - Clojure 中用什么来代替类和对象?你能举个例子吗?

javascript - GWT JSNI - Java 到 Javascript 返回 Java 导致未定义的参数

javascript - 为什么需要在对象中使用 this.property=property ?

python-3.x - 遍历列表字典并更新相应的列 - pandas