2011-02-15 32 views
3

在这个问题上What is the equivalent of the C++ Pair<L,R> in Java?,有可能我会在我的代码使用正确的类型转换(或任何其他方法)为Java泛型

public boolean equals(Object other) { 
    if (other instanceof Pair) { 
     Pair otherPair = (Pair) other; 
      return 
      (( this.first == otherPair.first || 
        (this.first != null && otherPair.first != null && 
         this.first.equals(otherPair.first))) && 
       (  this.second == otherPair.second || 
        (this.second != null && otherPair.second != null && 
        this.second.equals(otherPair.second)))); 
    } 

    return false; 
} 

Eclipse的警告我行“对这个示例代码otherPair =(Pair)other;“ “Pair是原始类型,对泛型类型的引用应该被参数化”。我尝试将它改写为“其他对”< A,B > otherPair =(对A,B >);“但警告仍然存在。如何正确地输入其他以便不会发生警告?谢谢!

+0

您是否尝试过参数化`instanceof`检查? – 2011-02-15 21:46:07

+0

我们可以看看班上的其他人吗? – 2011-02-15 21:47:58

回答

3

您可以使用

Pair<?, ?> otherPair = (Pair<?, ?>)other; 

由于平等接受一个对象,你不会有任何麻烦。在这个特定的代码中,类型参数是无关紧要的。