为什么for循环可以初始化Java中

时间:2018-04-28 01:51:41

标签: 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在0到1之间更新了swap,swap再次调用heapify1再次成功将numOfSwap = 1传递给heapify1。但为什么numOfSwap在迭代后在for循环中初始化?

1 个答案:

答案 0 :(得分:1)

Observable.fromIterable(imageList) .flatMap(image -> service.savePhoto(image)) .toList() .subscribeOn(Schedulers.io()) .subscribe(...) 方法有两种不同的上下文。

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

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