'E 即使在使用泛型之后也扩展了在 class Vector 中声明的对象

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

我已经尝试编译以下代码很长时间了,但我总是收到警告:

 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);

        }

    }
}

在这个程序中,我声明了一个 class Employee 并且旨在调用 Create 方法 n 次来添加 n 的细节员工之前,然后执行其他功能。 但是,我最初收到 Xlint:unhecked 警告,在使用 Xlint:unchecked 文件名再次编译后,我仍然收到此警告,无法继续进行。你能帮帮我吗?

您的 Create 方法中的参数 v 被声明为原始类型。 尝试将泛型类型添加到参数声明中,例如:

public static void Create(Vector<Employee> v, int n) {

您可以在以下内容中阅读有关原始类型的更多信息link:

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