1

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()+" ");
Cb173
  • 55
  • 1
  • 1
  • 8

1 Answers1

2

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<>();
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Okay I got it. The class name was the problem because It was vector and I need that to create the vector. Thank you so much!! I was completely lost and tried everything. @Yassin Hajaj – Cb173 May 28 '16 at 22:40
  • @CassandraB You're welcome Cassandra. Don't hesitate on mark the answer as accepted to let others know it's answered. :) – Yassin Hajaj May 28 '16 at 22:44
  • Last question: how could I Create a Vector object that can hold any type of object. – Cb173 May 28 '16 at 22:45
  • @CassandraB This is asking for trouble. I suggest you read this post, it answers the question : http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – Yassin Hajaj May 28 '16 at 22:51