我只是一个初学者,我需要一个好心人来帮助我;)我得到了这个方法并上线了:
( (HashSet<String>) pos[targetPos]).add(word);
它给了我一个异常(exception)
(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];
最佳答案
pos[]
定义为Object
大批。当您将其转换到HashSet<String>
时后来,Java 就不知道你可以这么做了。这就是未经检查的强制转换——编译器警告您可能正在做一些不安全的事情。
您可以通过更改 pos
的类型来消除警告。至HashSet<String>[]
.
关于java - Java中 "Unchecked cast"警告的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15953666/