英文:
'E extends Object declared in class Vector even after using generics
问题
以下是您提供的代码的翻译部分:
我一直在尝试编译以下代码已经有很长时间了,但我总是收到警告:
警告:[unchecked] 对未经检查的原始类型Vector的成员add
Element(E)的未经检查的调用
v.addElement(obj);
^
其中E是类型变量:
E扩展自类Vector中声明的Object
尽管我使用了泛型来声明我的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("输入员工人数");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Create(vec, n);
System.out.println("输入以下选项中的任意一项");
System.out.println("1 插入新记录");
System.out.println("2 按名称删除员工记录");
System.out.println("3 按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("输入ID");
eid = sc1.nextInt();
System.out.println("输入姓名");
ename = sc1.next();
System.out.println("输入薪水");
esal = sc1.nextFloat();
Employee obj = new Employee();
obj.name = ename;
obj.sal = esal;
obj.id = eid;
v.addElement(obj);
}
}
}
在这个程序中,我声明了一个名为Employee
的类,并计划在之前调用Create
方法n
次,以添加n
名员工的详细信息,然后执行其他功能。然而,我最初收到了Xlint:unchecked的警告,在使用Xlint:unchecked filename重新编译后,我仍然收到这个警告,无法继续进行。你能帮我解决一下吗?
英文:
I have been trying to compile the following code for a lot of time now, but I'm always getting the warning:
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
This is happening , even though I declared my Vector using generics. Could you please help me out ?
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);
}
}
}
In this program, I have declared a class Employee
and aim to call the Create
method n
times for adding the details of n
employees before and then performing other functions.
However I initiially got the Xlint:unhecked warning and after compiling it again with Xlint:unchecked filename I'm still getting this warning and am not able to proceed further. Could you please help me?
答案1
得分: 2
参数v
在您的Create
方法中被声明为原始类型。
尝试将泛型类型添加到参数声明中,例如:
public static void Create(Vector<Employee> v, int n) {
您可以在以下链接中阅读有关原始类型的更多信息:
https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
英文:
The argument v
in your Create
method is declared as a raw type.
Try adding the generic type to the parameter declaration, e.g.:
public static void Create(Vector<Employee> v, int n) {
You can read more about raw types in the following link:
https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论