public void test(int... integers, String str) { // error
...
}
The variable argument type int of the method test must be the last parameter.
public void test(String str, int... integers) {}
它運作良好。這有什麼理由嗎?
public void test(int... integers, String str) { // error
...
}
The variable argument type int of the method test must be the last parameter.
public void test(String str, int... integers) {}
它運作良好。這有什麼理由嗎?
好,考慮這個方法的簽名:
public void test(int... integers, float val, float val2)
現在,當你調用這個方法:
test(2, 3, 4, 5, 6, 7);
如何將編譯器決定何時停止添加參數int...
類型參數?請記住,參數是從Java中的left-to-right
開始評估的。這就是爲什麼var-args
應該在最後。因此,該編譯器可以首先分配固定參數,然後分配其餘參數,請轉至var-args
。
爲什麼編譯器不能看到6和7必須是浮點參數,因此2,3,4,5是整數?請注意,參數仍將在運行時從左到右進行評估。只要有一個可變參數,它可以在參數列表中的任何地方。其他一切都暗示了懶惰的編譯器編寫者。 – Ingo
http://stackoverflow.com/questions/2161912/why-varargs-should-be-the-last-in-method-signature – devnull
http://stackoverflow.com/questions/9372916/why-is-varargs -always-the-last-parameter-in-a-method-signature – devnull
謝謝@devnull,我想我應該刪除它:) –