我目前正在開發一個項目,並且我被困在這一點上。基本上我想弄清楚如何比較類型:鍵入類型:Int。從代碼中,here.theKey和theKey2是Key類型,Key1以key爲參數,獲取散列碼,然後用於在名爲'here'的BST中查找相同的散列碼。我遇到的問題是我無法弄清楚如何將this.theKey的值與作爲int的key1進行比較。將int值與Java中的不同類型進行比較
這裏是方法:
public Value get(Key key)
{
int theKey1 = hash(key);
TreeNode here = Tree;
while (here != null)
{
int theKey2=here.theKey;
if(theKey1 < theKey2)
{
here = here.left;
}
else if(theKey1>theKey2)
here = here.right;
else
{
return here.value;
}
}
throw new IllegalArgumentException("Value associated with the key not found");
}
我試着使用的compareTo但由於theKey1是int類型,我得到了一個錯誤。任何幫助或指導將不勝感激。
不應該這行'int theKey2 = here.theKey;'是'int theKey2 = hash(here.theKey);'? – Mritunjay
'int theKey2 = hash(here.theKey);',但是究竟是在hashcode上排序的BST的關鍵點? – EJP
該項目將編寫一個程序,該程序使用帶鏈接的Hashtable來處理碰撞,並使用BST代替陣列。 –