Cassandra B Cassandra B - 1 year ago 53
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

// 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:");
System.out.println(en.nextElement()+" ");

Answer Source

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 :


Change your class name

public class MyVector {}


Use the full classpath

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