Cassandra B Cassandra B - 6 months ago 11
Java Question

Creating a Vector Class object in Java

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()+" ");

Answer

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 full classpath

java.util.Vector<String> v = new java.util.Vector<>();
Comments