嗨,我有工作代碼,但我想打印出的座標。有一個包含座標和字符串的散列表。有一個座標類允許我放置座標,但是當我嘗試打印時,它會讓人感到困惑,我顯然沒有做正確的事情。感謝您的期待打印輸出座標hashmap
public class XYTest {
static class Coords {
int x;
int y;
public boolean equals(Object o) {
Coords c = (Coords) o;
return c.x == x && c.y == y;
}
public Coords(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int hashCode() {
return new Integer(x + "0" + y);
}
}
public static void main(String args[]) {
HashMap<Coords, String> map = new HashMap<Coords, String>();
map.put(new Coords(65, 72), "Dan");
map.put(new Coords(68, 78), "Amn");
map.put(new Coords(675, 89), "Ann");
System.out.println(map.size());
}
}
你必須重載的toString你的座標類 – Benoir 2012-08-02 19:37:00