‘E extends Object declared in class Vector even after using generics

huangapple go评论69阅读模式
英文:

'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&lt;Employee&gt; vec = new Vector&lt;Employee&gt;();
int n, ch;
System.out.println(&quot;Enter the number of employees&quot;);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Create(vec, n);
System.out.println(&quot;Enter any 1 of the following choices &quot;);
System.out.println(&quot;1 to insert a new record&quot;);
System.out.println(&quot;2 to delete an Employee record by name&quot;);
System.out.println(&quot;3 to delete by the ID&quot;);
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 &lt; n; i++) {
System.out.println(&quot;Enter the ID&quot;);
eid = sc1.nextInt();
System.out.println(&quot;Enter the name&quot;);
ename = sc1.next();
System.out.println(&quot;Enter the salary&quot;);
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&lt;Employee&gt; 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&lt;Employee&gt; v, int n) {

You can read more about raw types in the following link:

https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html

huangapple
  • 本文由 发表于 2020年9月14日 23:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定