小弟新手,写了一段代码编译时发出泛型警告,不知道如何解决,望高手帮忙。
import java.util.*;
class Employee
{
private String name;
private double salary;
private Date hireDay; public Employee(String n,double s,int year,int month,int day)
{
name = n;
salary = s;
GregorianCalendar c = new GregorianCalendar(year,month - 1,day);
hireDay = c.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;
}
}
//上面的应该没错,错误应该在下面这段
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList staff = new ArrayList();
staff.add(new Employee("zhangsan",4000,1973,3,3));
staff.add(new Employee("lise",5000,1983,4,4));
staff.add(new Employee("wangwu",6000,1977,6,15)); for(int i = 0; i < staff.size(); i++)
{
Employee e = (Employee)staff.get(i);
e.raiseSalary(15);
} 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());
}
}
}
import java.util.*;
class Employee
{
private String name;
private double salary;
private Date hireDay; public Employee(String n,double s,int year,int month,int day)
{
name = n;
salary = s;
GregorianCalendar c = new GregorianCalendar(year,month - 1,day);
hireDay = c.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;
}
}
//上面的应该没错,错误应该在下面这段
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList staff = new ArrayList();
staff.add(new Employee("zhangsan",4000,1973,3,3));
staff.add(new Employee("lise",5000,1983,4,4));
staff.add(new Employee("wangwu",6000,1977,6,15)); for(int i = 0; i < staff.size(); i++)
{
Employee e = (Employee)staff.get(i);
e.raiseSalary(15);
} 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());
}
}
}
例:
List<String> l=new ArrayList<String>();
表明这个List中存的所有元素都是String类型,如果程序员不小心把其它类型存进去,编译会报错,帮助程序员检查错误。
由于集合通常是用来存相同类型对象的,所以这个改进还是有一定意义的,但JAVA的集合能存不同的数据类型也是JAVA的一大优势,所以泛型是选用的,可以不用。你用的是1.5以后的JDK,编译器建议程序员使用泛型,而你没使用,所以会有警告,但不影响程序的编译和运行。