MrS*_*ent 0 java eclipse vector
我正在学习Java,到目前为止我已经达到了Vector
s,但我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
有什么问题?我必须下载什么吗?
在命名你自己的类时,Vector
你正在隐藏名称java.util.Vector
- 所以你所做的每个不合格的引用Vector
意味着你自己的类 - 而且没有(int,int)
--constructor.
为了解决这个问题,您在技术上有两个选择:
要么合格每次使用java.util.Vector
喜欢
java.util.Vector v = new java.util.Vector(3,2);
将您自己的类重命名为MyVectorTestProgram
类似的东西
但是第一个并不是一个真正有效的选项 - 使用与java-library的核心类相同的名称是非常糟糕的编程风格,因为这几乎总会产生你现在遇到的同样问题.
归档时间: |
|
查看次数: |
1394 次 |
最近记录: |