2011-02-15 29 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; 

由於平等接受一個對象,你不會有任何麻煩。在這個特定的代碼中,類型參數是無關緊要的。