嗨,我有工作代码,但我想打印出的坐标。有一个包含坐标和字符串的散列表。有一个坐标类允许我放置坐标,但是当我尝试打印时,它会让人感到困惑,我显然没有做正确的事情。感谢您的期待打印输出坐标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