2017-07-01 89 views
0

在下面的Java代碼:爲什麼這個for-loop被跳過?

public static void main(String[] args) { 
    Integer[] k = new Integer[] {0, 1, 2, 3}; 

    int m = 0; 
    while (m < k.length) { 
     for (int i = 0; i < k.length && k[i] != null; i++) 
      System.out.print(k[i] + " "); 
     System.out.println(""); 
     k[m++] = null; 
    } 
} 

我試圖讓下面的輸出:

0 1 2 3 
1 2 3 
2 3 
3 

然而,內部的for循環的第一遍後一起跳過,也就是說,後0 1 2 3的輸出,這是因爲第一個元素在第一次傳遞後被設置爲null,但我不明白爲什麼這很重要,有人能解釋一下嗎?真的很困惑。提前致謝。 :)

+7

你知道什麼'K [i] = null'在環測試手段!? – user2357112

+1

And ...你知道'k [m ++] = null'是什麼嗎? –

+0

我強烈建議學習使用您的調試器。 –

回答

1

如果您使用i = m而不是i=0那麼它將工作。

public static void main(String[] args) { 
    Integer[] k = new Integer[] {0, 1, 2, 3}; 

    int m = 0; 
    while (m < k.length) { 
     for (int i = m; i < k.length && k[i] != null; i++) 
      System.out.print(k[i] + " "); 
     System.out.println(""); 
     k[m++] = null; 
    } 
+0

你可以刪除'k [m ++] = null;'(使其成爲'm ++'),並消除'k [i]!= null'測試。 –

+0

當然,在這種情況下它是相當多餘的,我只是想讓答案集中在他的實際錯誤上。 –

0

i < k.length && k[i] != null這使得for循環停止。您指數(i)開始於0,因此循環的你第一次循環的第二次時,以下情形後:

k = {null, 1, 2, 3} 
m = 1 

m < k.length = true 

i = 0 
i < k.length = true 

// k = {null, 1, 2, 3} 
// i = 0 
// k[i] = k[0] = null 
k[i] != null = false 
1

嘗試去通過代碼迭代,每次迭代和更新的值:

讓我們假設我們到達的第一次這行代碼:

// Right now, (**before** the next line is executed), 
// `m` is still `0`, `k.length` is 4 (which is how we reached this point). 
// `i` is 0, which is less than `k.length`, and `k[i]` is `0`. 

// This line sets `k[m]` to `null`, and then sets `m` to `m+1`. 
k[m++] = null; 

// And now (**after** the above line is executed), `m` is 1, 
// `k.length` is still 4, so we get to go into the while loop for the second time. 
// At this point, `k: [null, 1, 2, 3]`. 

在while循環中,我們再次遇到內環,這(這是我們第二時間在這條線)是:

// ...in the while loop for the second time... 
// Remember, the k array now looks like: k: [null, 1, 2, 3] 
for (int i = 0; i < k.length && k[i] != null; i++) 

你的狀態在這一點上失敗,因爲:

// Evaluating: i < k.length && k[i] != null 
i < k.length // i: 0, k.length: 4, this is true 
k[i] != null // i: 0 here, and k[0]: `null`, so this is false 

true && false // returns false -> Don't go in the inner for-loop 
+0

感謝您的詳細解釋。事後看來,事實如此明顯,確實是大腦放屁。再次感謝你 –

0

我不明白爲什麼你不只是使用兩個for循環,這是一個有點清晰:

public static void main(String[] args) { 
    Integer[] k = new Integer[] {0, 1, 2, 3}; 

    for (int m = 0; m < k.length; m++) { 
     for (int i = 0; i < k.length && k[i] != null; i++) { 
      System.out.print(k[i] + " "); 
     } 
     System.out.println(""); 
     k[m] = null; 
    } 
} 

問題陳述是你的內循環,它檢查是否k[i] != nullfor循環的第二條語句是終止條件。如果它返回false,則循環結束。實質上,如果它們爲null,則試圖不打印值,但這會在達到空值時終止您的循環。取而代之的是,檢查內循環內的元件的條件:

for (int i = 0; i < k.length; i++) { 
    if (k[i] != null) { 
     System.out.print(k[i] + " "); 
    } 
} 

此外,可以甚至跳過基於使用該內循環現在(int i = m),而不是設置爲空當前索引,所以在第二次迭代是1 -3,第三個是2-3,等等。此外,還可以使空白檢查/設置爲冗餘,並允許您使用int[]。因此,最終的解決方案是這樣的:

public static void main(String[] args) { 
    int[] k = new int[] {0, 1, 2, 3}; 

    for (int m = 0; m < k.length; m++) { 
     for (int i = m; i < k.length; i++) { 
      System.out.print(k[i] + " "); 
     } 
     System.out.println(""); 
    } 
} 
0

問題是由這些語句引起的:

k[i] != null;

k[m++] = null;

如果你設置數組中的下一個元素是nullfor循環中的空檢查條件將失敗,循環將不會執行。

刪除這兩個語句和這樣做:

class Test { 
    public static void main(String args[]) { 
     int k[] = {0, 1, 2, 3}; 
     int m = 0; 
     while (m < k.length) { 
      for (int i = m; i < k.length; i++) 
       System.out.print(k[i]); 
      System.out.print("\n"); 
      m++; 
     } 
    } 
}