public class server_run{
class constatnt_Cells{
private int x;
private int y;
public constatnt_Cells(int fx , int fy) {
x = fx;
y = fy;
}
}
private static Map <constatnt_Cells,Integer> test = new HashMap<constatnt_Cells,Integer>();
}
我想創建Integer
hashmap
這constatnt_Cells
關鍵和價值,但現在的問題是靜態部分dose't工作在這種情況下 我解釋我的問題用一個例子
constatnt_Cells goldCell = new constatnt_Cells(2,6);
System.out.println(test.get(goldCell));
test.put(goldCell, 50);
該代碼總是打印空,但如果我改變的hashmap
鍵的類型,例如我nteger
System.out.println(test.get(10));
test.put(10, 50);
它只能打印空一次之後,它總是打印那麼究竟是什麼constatnt_Cells
類,並在這種情況下Integer
類之間的差別。
我所有的代碼是在server_run
類,並像它的名稱服務器運行這個類中的每個第二
預計爲什麼你在put()之前使用'get()'? – Codebender
,因爲我想看看我的靜態哈希映射是否工作,如果它只是在第一秒工作,它打印空和其他時間應該打印50 –
你需要重寫equals和hashcode方法。除非你這樣做,否則首先創建的所有實例都應該是不同的。 – SacJn