【发布时间】:2016-07-13 03:20:18
【问题描述】:
对不起,如果这是基本的,但我正在启动 java,这不是任何任务,但为什么我的简单测试程序失败了?
static public class pair
{
public int x;
public int y;
public pair(int a , int b)
{
x = a;
y = b;
}
}
public static void main(String args[])
{
HashMap<pair,Integer>m = new HashMap<pair,Integer>();
m.put(new pair(1,2), 3);
if(m.containsKey(new pair(1,2)))
{
System.out.println("is there " + m.get(new pair(1,2)));
}
}
【问题讨论】:
-
这是一个类似的问题:stackoverflow.com/questions/12774723/…
-
如果你还没有覆盖
equals和hashCode那么你是说每个对象都是不同的。 -
我建议您阅读本文以了解 Java 代码风格 google.github.io/styleguide/javaguide.html
-
如果你只是从Java开始,最好从头开始