Advertisement

[java] Vector problem

Started by July 09, 2001 11:15 AM
3 comments, last by WoX 23 years, 7 months ago
Vector sample = new Vector(0,1); int number = 5; sample.addElement(number); when trying the code above I get this error: addElement(java.lang.Object) in java.util.Vector cannot be applied to (int) sample.addElement(number) ^ Have I forgotten to import something or what? Here''s what I''ve already imported: import java.applet.Applet; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.util.*; import java.lang.Math; import java.net.*; import java.io.*; import java.awt.event.*; import java.util.Vector.*; import java.lang.*; import java.*; also I implements an actionlistener like: public class tetris extends Applet implements Runnable, ActionListener can that have anything to do with it? thanks for any replies!
The problem is that a Vector object can only store objects of the type Object (or subclasses thereof), which int is not. You have to use the Integer class to encapsulate the int into a subclass of Object:

sample.addElement(new Integer(number)) should work

Check the API docs for more info on the Integer type.
Advertisement
hey it worked
thanks man
that above was me
I just wanted to mention that if you are using Java 1.2 or higher, you may
want to use an ArrayList rather than a Vector. ArrayList is the equivilant
to the vector class in Java''s collection framework. ArrayList does not
synchronize around every method call like a Vector. The synchronized
implementation of Vector is slower than the unsynchronized ArrayList. Of
course this may be a mute point if you expect multiple threads will
concurrently access your data structure or if you are using Java 1.1 or
below.

Tyler.
vangordr@pacbell.net

This topic is closed to new replies.

Advertisement