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

ArrayList staff = new ArrayList();

 

在编译java源文件时,如果你的jdk是1.5或者以上,会出现“使用了未经检查或不安全的操作;请使用 -Xlint:unchecked 重新编译。”这样的警告,原因是JDK 1.5中集合类的实例创建方式与JDK 1.4的有一些不同,JDK 1.5 里面引入很多新的特性。当你使用诸如java.util.Hashtable 、java.util.ArrayList等收集类(Collection)时,需要注意 JDK 1.5 里面支持所谓的 Generic Class,也就是参数化类型,如果没有指定类型参数(JDK 1.5 以前的编码),则 JDK 1.5 编译器由于无法检查给出的参数是否合乎要求,而报告 unchecked 警告,这并不影响运行。按照提示,编译是指定参数即可取消这样的警告。或者为其制定类型参数。修改成如下代码即可:

 

import java.util.*;

 

public class ArrayListTest

{

    public static void main(String[] args)

    {

        ArrayList staff = new ArrayList();

        staff.add(new Employee("Calr Cracker", 70000, 1987, 12, 15));

        staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));

        staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));

 

        for (int i = 0; i < staff.size(); i++)

        {

            Employee e = (Employee)staff.get(i);

            e.raiseSalary(5);

        }

 

        for (int i = 0; i < staff.size(); i++)

        {

            Employee e = (Employee)staff.get(i);

            System.out.println("name=" + e.getName()

                 + ",salary=" +e.getSalary()

                 + "hireDay=" +e.getHireDay());

        }

    }

}

 

class Employee

{

    public Employee(String n, double s, int year, int month, int day)

    {

        name = n;

        salary = s;

        GregorianCalendar calendar = new GregorianCalendar(year, month - 1,day);

        hireDay = calendar.getTime();

    }

 

    public String getName()

    {

        return name;

    }

 

    public double getSalary()

    {

        return salary;

    }

 

    public Date getHireDay()

    {

        return hireDay;

    }

 

    public void raiseSalary(double byPercent)

    {

        double raise = salary * byPercent / 100;

        salary += raise;

    }

 

    private String name;

    private double salary;

    private Date hireDay;

}

文章出自:http://molutran.blog.163.com/blog/static/10252321620090810052199/

你可能感兴趣的:(Java)