我在Eclipse中遇到問題。爲什麼LogCat中oldList
的值不同,而我在兩個Log命令之間沒有更改它的值?java中的變量不正確
首先我有一個initialize
方法:
private void initialize() {
list[0][0] = 2;
list[0][1] = 4;
list[1][0] = 3;
list[1][1] = 7;
oldList = list;
going();
}
並在going
方法,我印刷oldList
兩次:
private void going() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Log.i("Log", "oldList = " + oldList[i][j]);
}
}
Log.i("Log", "---------------------------");
// ----------------------------------------------------
list[0][0] = 0;
list[0][1] = 5;
list[1][0] = 0;
list[1][1] = 0;
// ----------------------------------------------------
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Log.i("Log", "oldList = " + oldList[i][j]);
}
}
}
但兩個結果是在logcat的不同:
oldList = 2
oldList = 4
oldList = 3
oldList = 7
---------------------------
oldList = 0
oldList = 5
oldList = 0
oldList = 0
雖然我不改變它之間的兩個日誌。我只是改變了list
的值,而不是oldList
。爲什麼輸出會改變?
[?就是Java「傳址參考」或「傳遞的價值」(http://stackoverflow.com/questions/40480/is- java-pass-by-reference-or-pass-by-value) – zapl