我的目的是向HashSet中填充数据,然后再将它打印出来。但是编译过程中老是说我进行了不安全的操作,详细信息请用-Xlint:unchecked重新编译这到底是怎么回事呢?请高人给解释一下
import java.util.*;
class Test
{
static Set fill(Set s, int size)
{
for(int i=1;i<=size;i++)
{
s.add(new Integer(i));
}
return s;
}
static void test(Set s)
{
fill(s,5);
System.out.println(a);
}
public static void main(String args[])
{
test(new HashSet());
} }
import java.util.*;
class Test
{
static Set fill(Set s, int size)
{
for(int i=1;i<=size;i++)
{
s.add(new Integer(i));
}
return s;
}
static void test(Set s)
{
fill(s,5);
System.out.println(a);
}
public static void main(String args[])
{
test(new HashSet());
} }
Set s 改成 Set<Integer> s
test(new HashSet<Integer>())改成
就可以了
Set s 改成 Set<Integer> s;
static void test(Set s)改成static void test(Set<Integer> s);
test(new HashSet())改成test(new HashSet<Integer>())
就可以了
static Set<Integer> fill(Set<Integer> s, int size)
楼主,这句能编译通过?a都没定义过
能给我解释一下是什么原因么???谢谢
我想强制你“标明”数据类型的的好处是让你清楚你在做什么,增强了你程序的可读性。
而且这样作的话,数据取出来就不需要强制转换了偶菜菜,说的很肤浅,期待高人给深入讲解下