如何解决'E即使使用了泛型后,也会扩展Vector类中声明的对象
我已经尝试编译以下代码很多时间了,但是我总是收到警告:
warning: [unchecked] unchecked call to addElement(E) as a member of the raw type Vector
v.addElement(obj);
^
where E is a type-variable:
E extends Object declared in class Vector
这是发生的,即使我使用泛型声明了Vector。你能帮我吗?
import java.util.*;
public class Employee {
String name;
float sal;
int id;
public static void main(String args[]) {
Vector<Employee> vec = new Vector<Employee>();
int n,ch;
System.out.println("Enter the number of employees");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Create(vec,n);
System.out.println("Enter any 1 of the following choices ");
System.out.println("1 to insert a new record");
System.out.println("2 to delete an Employee record by name");
System.out.println("3 to delete by the ID");
ch = sc.nextInt();
switch (ch) {
case 1:
{
}
case 2:
{
}
}
}
public static void Create(Vector v,int n) {
String ename;
float esal;
int eid;
int i;
Scanner sc1 = new Scanner(System.in);
for (i = 0; i < n; i++) {
System.out.println("Enter the ID");
eid = sc1.nextInt();
System.out.println("Enter the name");
ename = sc1.next();
System.out.println("Enter the salary");
esal = sc1.nextFloat();
Employee obj = new Employee();
obj.name = ename;
obj.sal = esal;
obj.id = eid;
v.addElement(obj);
}
}
}
在此程序中,我声明了一个类Employee
,旨在调用Create
方法n
次,以在之前添加n
名员工的详细信息,然后执行其他操作。职能。
但是,我最初收到Xlint:unhecked警告,并再次使用Xlint:unchecked文件名编译后,我仍然收到此警告,无法继续进行。你能帮我吗?
解决方法
您的v
方法中的参数Create
被声明为原始类型。
尝试将泛型添加到参数声明中,例如:
public static void Create(Vector<Employee> v,int n) {
您可以在以下链接中了解有关原始类型的更多信息:
https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。