Java 从hashmap打印坐标
嗨,我有工作代码,但我想打印出坐标。有一个包含坐标和字符串的hashmap。有一个坐标类,允许我输入坐标,但当我试图打印出来时,会感到困惑,我显然没有做正确的事情。谢谢你的关注Java 从hashmap打印坐标,java,hashmap,coordinates,println,Java,Hashmap,Coordinates,Println,嗨,我有工作代码,但我想打印出坐标。有一个包含坐标和字符串的hashmap。有一个坐标类,允许我输入坐标,但当我试图打印出来时,会感到困惑,我显然没有做正确的事情。谢谢你的关注 public class XYTest { static class Coords { int x; int y; public boolean equals(Object o) { Coords c = (Coords) o; return c.x == x &
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());
}
}
公共类测试{
静态类坐标{
int x;
int-y;
公共布尔等于(对象o){
坐标c=(坐标)o;
返回c.x==x&&c.y==y;
}
公共坐标(整数x,整数y){
超级();
这个.x=x;
这个。y=y;
}
公共int hashCode(){
返回新的整数(x+“0”+y);
}
}
公共静态void main(字符串参数[]){
HashMap=newHashMap();
普特地图(新坐标(65,72),“丹”);
地图放置(新坐标(68,78),“Amn”);
地图放置(新坐标(675,89),“安”);
System.out.println(map.size());
}
}
您必须在Coords类中重写toString()
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 String toString()
{
return x + ";" + y;
}
}
让你困惑的是:
XYTest.Coords@3e25a5
这是什么?它是原始toString()
方法的结果。它的作用如下:
return getClass().getName() + '@' + Integer.toHexString(hashCode());
因此,使用您自己的代码覆盖它将消除令人困惑的输出:)
请注意,您的哈希冲突非常严重。更好的hashCode()实现是:
public int hashCode()
{
return (x << 16) ^ y;
}
public int hashCode()
{
return(x您必须在Coords类中重写toString()
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 String toString()
{
return x + ";" + y;
}
}
让你困惑的是:
XYTest.Coords@3e25a5
这是什么?它是原始toString()
方法的结果。它的作用如下:
return getClass().getName() + '@' + Integer.toHexString(hashCode());
因此,使用您自己的代码覆盖它将消除令人困惑的输出:)
请注意,您的哈希冲突非常严重。更好的hashCode()实现是:
public int hashCode()
{
return (x << 16) ^ y;
}
public int hashCode()
{
返回(x如Martijn所说覆盖toString()
}
…和
public static void main(String []args){
正如Martijn所说,重写toString()
}
…和
public static void main(String []args){
你必须在你的坐标类中重写toString你必须在你的坐标类中重写toString我尝试了这个修复,但不起作用它似乎不喜欢public void。然后当我尝试将其更改为public string时,hashmap不允许它。我在创建hashmap时是否必须更改某些内容?别担心,我意识到该做什么了,我没有把它放到map.put函数中,但是把它放到println函数中,谢谢help@Djchunky123:ooops,你是对的。我犯了一个错误,当然void
应该被String
替换。我尝试了这个修复方法,但不起作用。它似乎不喜欢public void。然后当我尝试将其更改为public String时,hashmap不起作用i don’我不允许这样做创建Hashmap时我必须更改一些内容吗?别担心,我没有将它放在map.put函数中,而是放在println函数中谢谢help@Djchunky123:Oooops,你是对的。我犯了一个错误,当然void
应该被String
替换。嗨,谢谢你的帮助,但是你的Hashmap不允许在映射中字符串。put函数你在打印函数中使用它,谢谢你的帮助:)嗨,谢谢你的帮助,但是你的Hashmap不允许在映射中字符串。put函数你在打印函数中使用它,谢谢你的帮助:)