我已經問過這個問題,並且我還有另外一個關於使用 我自己做的類的問題。 請參閱下面的代碼。java對和ArrayList問題
import java.util.*;
class Pair{
int toWhere;
int weight;
}
public class Test{
public static void main(String[] args){
ArrayList[] arr = new ArrayList[2];
Pair p = new Pair();
for(int i=0; i<arr.length; i++)
arr[i] = new ArrayList<Pair>();
p.toWhere = 1;
p.weight = 2;
arr[0].add(p);
System.out.println(p); // gives me [email protected]
System.out.println(arr[0].get(0)); // gives me exactly the same, [email protected]
System.out.println(p.toWhere); // gives me no error, and is 1
System.out.println(arr[0].get(0).toWhere); // gives me an error
}
}
我的問題是這樣的。 p的值和arr[0].get(0)
(這是地址?我猜)是一樣的。 但爲什麼p.toWhere
給我的準確數值和 arr[0].get(0).toWhere
不?
可能是你應該使用這個,而不是'ArrayList的 []' –
Nishant