我有一个 IntPair 类,它有两种我从中使用的方法:“getFirst()”和“getSecond()”。虽然我目前正在使用这种方法,但我想检查“hashMap j”是否包含特定值,然后执行操作。我认为我在这些方面有问题:
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
我不知道我是否将 ok 转换为 Object,或者第一行“Object obj = j.values()”是否应该替换为另一个方法调用。我在 j.containsValue("0") 之后使用 System.out.print ("Message") 进行了测试,我收到了消息。
这是我试图使其发挥作用的方法的一部分。
public static HashMap<IntPair, String> j = new HashMap<>();
j.put(new IntPair(firstInt, secondInt), value);
if (j.containsValue("0"))
{
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
t.putCharacter('x');
}
else if (j.containsValue("1"))
{
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
t.putCharacter('v');
}
国际对类:
public class IntPair {
private final int first;
private final int second;
public IntPair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + this.first;
hash = 89 * hash + this.second;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final IntPair other = (IntPair) obj;
if (this.first != other.first) {
return false;
}
if (this.second != other.second) {
return false;
}
return true;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
任何帮助将非常感激。谢谢!