java错误:使用了未经检查或不安全的操作

来源:互联网 发布:tower软件下载 编辑:程序博客网 时间:2024/06/04 18:28

出现这个警告 但不影响程序运行

在网上查了查 原因如下

在编译java源文件时,你使用的是jdk1.5或以上时,可能出现这个问题。

(使用了未经检查或不安全的操作;请使用 -Xlint:unchecked 重新编译。)

     原因是jdk1.5里的集合类的创建和jdk1.4里有些区别,主要是jdk1.5里增加了泛型,也就是说可以对集合里的数据进行检查。在jdk1.5以前,如果没有指定参数类型,则 JDK 1.5 编译器由于无法检查 给出的参数是否合乎要求,而报告 unchecked 警告,这并不影响运行。按照提示,编译是指定参数即可取消这样的警告。或者为其制定类型参数。

代码如下

public class jigexuesheng
{
//需要import java.util.ArrayList;
public static ArrayList
<String> jiGeXueSheng(Student[] students) //返回及格学生列表的方法
{
ArrayList
<String> myList =new ArrayList<String>();
   for(int i=0;i<students.length;i++)
   {
    if( students[i].testScore()<60 )
    {
     continue;
    }
    myList.add(students[i].studentName);
   }
   return myList;
  
}

public static void main(String args[])
{
   Student[] students = new Student[5]; //定义一个学生类向量 存放一些学生的信息

   //从stuInfo.dat文件中读出
   try
   {
    FileInputStream fi=new FileInputStream("stuInfo.dat");
    ObjectInputStream si=new ObjectInputStream(fi);
    for(int i=0;i<5;i++)
     students[i]=(Student)si.readObject();
    si.close();
   }
   catch(Exception e)
   {
    System.out.println(e);
   }
   //输出学生信息
   for(int i=0;i<5;i++)
   {
    System.out.println(students[i]);
   }
  
  
   //返回及格学生列表
ArrayList <String>myList=new ArrayList <String>();
   myList=jiGeXueSheng( students);
   System.out.println(myList);
}

蓝色部分为修改的部分

只要在<>内说明容器ArrayList中存的类型,警告能去除


本文举的是ArrayList一类容器,其他容器类似用法

原创粉丝点击