我只是一个初学者,我希望有一个好的灵魂来帮助我;)我得到了这个方法并上线了:
( (HashSet<String>) pos[targetPos]).add(word);
它给了我一个例外
(Unchecked cast from Object to HashSet<String>)
我试图将 Object[pos] 更改为 String[pos] 以更具体,但它在这一行给我一个错误:pos[targetPos] = new HashSet<String>();
Type mismatch: cannot convert from HashSet<String> to String
这是方法:
public void add(String word, Object[] root){
Object[] pos = root;
int wordIndex = 0;
int targetPos;
if(word.length()>=3){
for(int i = 1; i <=3; i++){
targetPos = word.charAt(wordIndex) -'a'; //convert a letter into index eg a==0
if(i==3){
if(pos[targetPos]==null){
pos[targetPos] = new HashSet<String>();
}
( (HashSet<String>) pos[targetPos]).add(word);
//System.out.println(Arrays.toString(pos));
break;
}//end if outer if
else{
if(pos[targetPos]==null){
pos[targetPos] = new Object[28];
}
wordIndex++;
pos = (Object[]) pos[targetPos];
}
}//end of for
}
}
根是
Object[] root = new Object[28];