Java Eclipse中的未定义向量

Java Eclipse中的未定义向量,java,eclipse,vector,Java,Eclipse,Vector,我正在学习Java,到目前为止,我已经接触到Vectors,但是我的IDE似乎给了我一些问题,所以我想请您为我的问题提供一个可能的解决方案 import java.util.*; public class Vector { public static void main(String args[]) { // initial size is 3, increment is 2 Vector v = new Vector(3, 2); Sy

我正在学习Java,到目前为止,我已经接触到Vectors,但是我的IDE似乎给了我一些问题,所以我想请您为我的问题提供一个可能的解决方案

import java.util.*;

public class Vector {
    public static void main(String args[]) {
        // initial size is 3, increment is 2
        Vector v = new Vector(3, 2);
        System.out.println("Initial size: " + v.size());
        System.out.println("Initial capacity: " + v.capacity());
        v.addElement(new Integer(1));
        v.addElement(new Integer(2));
        v.addElement(new Integer(3));
        v.addElement(new Integer(4));
        System.out.println("Capacity after four additions: " + v.capacity());

        v.addElement(new Double(5.45));
        System.out.println("Current capacity: " + v.capacity());
        v.addElement(new Double(6.08));
        v.addElement(new Integer(7));
        System.out.println("Current capacity: " + v.capacity());
        v.addElement(new Float(9.4));
        v.addElement(new Integer(10));
        System.out.println("Current capacity: " + v.capacity());
        v.addElement(new Integer(11));
        v.addElement(new Integer(12));
        System.out.println("First element: " + (Integer) v.firstElement());
        System.out.println("Last element: " + (Integer) v.lastElement());
        if (v.contains(new Integer(3)))
            System.out.println("Vector contains 3.");
        // enumerate the elements in the vector.
        Enumeration vEnum = v.elements();
        System.out.println("\nElements in vector:");
        while (vEnum.hasMoreElements())
            System.out.print(vEnum.nextElement() + " ");
        System.out.println();
    }
}
我得到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The constructor Vector(int, int) is undefined
    The method size() is undefined for the type Vector
    The method capacity() is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method capacity() is undefined for the type Vector
    The method addElement(Double) is undefined for the type Vector
    The method capacity() is undefined for the type Vector
    The method addElement(Double) is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method capacity() is undefined for the type Vector
    The method addElement(Float) is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method capacity() is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method addElement(Integer) is undefined for the type Vector
    The method firstElement() is undefined for the type Vector
    The method lastElement() is undefined for the type Vector
    The method contains(Integer) is undefined for the type Vector
    The method elements() is undefined for the type Vector

    at Vector.main(Vector.java:7)

问题是什么?我必须下载什么吗?

由于您的类名也是Vector,编译器无法解析java.util.Vector。 将代码更改为

java.util.Vector v = new java.util.Vector(3, 2);

不完全确定您是否有运行时错误或编译错误,但您通过导入包含java.util.Vector的java.util.*并调用您的类Vector来混淆编译器。最好重命名您的类。

在命名您自己的类向量时,您隐藏了java.util.Vector的名称-因此您对Vector的每个非限定引用都意味着您自己的类-并且没有int,int构造函数

要解决这一问题,您在技术上有两种选择:

要么限定java.util.Vector的每次使用,就像

java.util.Vector v=new java.util.Vector 3,2

将您自己的类重命名为MyVectorTestProgram或类似的东西

但是第一个选项并不是一个真正有效的选项——使用与java库的核心类相同的名称是非常糟糕的编程风格,因为这几乎总是会产生与您现在遇到的问题完全相同的问题。

您的类也被命名为“Vector”。它在Java中隐藏声明

见:

名为n的类型的声明d隐藏了任何 在d出现点的作用域中名为n的其他类型 在d


当您希望编译器使用java.util.Vector时,它正在使用Vector类。删除import语句,然后运行organizeimports命令


要么将类名更改为Vector以外的名称,要么使用java.util.Vector v=new java.util.Vector3,2;。顺便说一句,我强烈推荐第一种解决方案,而不是第二种。一个一般性建议:如果有编译错误,不要运行程序。首先解决这些错误。顺便说一句,不管你用什么学习材料,扔掉它,寻找更好的,因为它似乎已经过时了。15年前,此API已被Collection API取代。十年前就不再需要调用新整数、新浮点或新双精度。这将实现什么?并没有一个组织的进口将使这项工作;他或者需要引用完全限定名java.util.Vector,或者重命名他的测试类。