Creating a Vector Class object in Java

2019-07-28 03:06发布

The Problem with this is it gives me an error message saying The type Vector is not generic; it cannot be parameterized with arguments . However I need the argument types. Keep in mind that I'm new to java.

 package day7; 

 import java.util.*;



    public class Vector {

      public static void main(String args[]) {

              //Vector        //vec     //Vector throws the error. 
       public Vector<String> vec = new Vector<String>(50);

       Vector v = new Vector();

       //Adding elements to a vector 
       vec.addElement("Apple");
       vec.addElement("Orange");
       vec.addElement("Mango");
       vec.addElement("Fig");

        // check size and capacityIncrement
        System.out.println("Size is: "+vec.size());
        System.out.println("Default capacity increment is: "+vec.capacity());


       Enumeration en = vec.elements();
       System.out.println("\nElements are:");
       while(en.hasMoreElements())
           System.out.println(en.nextElement()+" ");

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-28 03:23

Your class shouldn't be named Vector. Otherwise, the compiler will refer to it even if you're trying to use java.util.Vector.

One of the two following solutions :

#1

Change your class name

public class MyVector {}

#2

Use the fully qualified class name

java.util.Vector<String> v = new java.util.Vector<>();
查看更多
登录 后发表回答