关于网友提出的“ HashSet应用时的警告,如何去除?”问题疑问,本网通过在网上对“ HashSet应用时的警告,如何去除?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: HashSet应用时的警告,如何去除?
描述:Set set = new HashSet();报的警,如下:
Multiple markers at this line
- Type safety: The expression of type HashSet needs unchecked conversion to conform to
Set
- HashSet is a raw type. References to generic type HashSet should be parameterized
Multiple markers at this line
- HashSet is a raw type. References to generic type HashSet should be parameterized
- Type safety: The expression of type HashSet needs unchecked conversion to conform to
Set
import java.util.HashSet;
import java.util.Set;
class FindSame {
public static void main(String args[]) {
System.out.println("This is a simple java program");
int arrayInt[] = {1, 2, 1, 3, 4, 6, 7};
checkSameIntItems(arrayInt);
String arrayStr[] = {"1", "2", "1", "3", "4", "6", "7"};
checkSameStrItems(arrayStr);
}
private static void checkSameIntItems(int[] array) {
Set set = new HashSet();
boolean bCheckSame = false;
for (int i=0; i<>
if (!set.add(array[i])) {
bCheckSame = true;
}
}
System.out.println("The hash items:");
for (Object o : set) {
System.out.println(o);
}
if (bCheckSame) {
System.out.println("The array has same item");
}
else {
System.out.println("The array has not same item");
}
}
private static void checkSameStrItems(String[] array) {
Set set = new HashSet();
boolean bCheckSame = false;
for (int i=0; i<>
if (!set.add(array[i])) {
bCheckSame = true;
}
}
System.out.println("The hash items:");
for (Object o : set) {
System.out.println(o);
}
if (bCheckSame) {
System.out.println("The array has same item");
}
else {
System.out.println("The array has not same item");
}
}
}
解决方案1: 集合的警告是泛型未检查。。。。你可以给他加上泛型 17 行 Set set = new HashSet(); 或者用注解@SuppressWarnings("unchecked")。。。。但是后者不建议使用
以上介绍了“ HashSet应用时的警告,如何去除?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1848646.html