gpt4 book ai didi

java - Eclipse 中未定义的 vector

转载 作者:行者123 更新时间:2023-12-01 06:38:19 28 4
gpt4 key购买 nike

我正在学习 Java,到目前为止我已经达到了 Vector 的水平,但是我的 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 的名称 - 因此您对 Vector 进行的每个不合格引用表示您自己的类 - 并且没有 (int,int) 构造函数。

要解决这个问题,从技术上讲,您有两种选择:

  • java.util.Vector 的每次使用进行限定,例如

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

  • 将您自己的类重命名为 MyVectorTestProgram 或类似名称

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

关于java - Eclipse 中未定义的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26419951/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com