• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a vector  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I am trying to create a vector:

Vector myVector = new Vector();

When I try an add an element to the end:

myVector.addElement(3);

and I compile I get the following message:

Note: C:\Vector.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

So, when I do that I get:

Vector.java:94: warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector myVector.addElement(currentID);
^

What am i doing wrong? I cant seem to find any help anywhere

Thanks
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a warning one gets when one uses Collection Framework classes
which have been generified in Java 5.
either you can ignore the warning or use Java 1.4 compiler.
or learn about generics and use them.
 
Saket Barve
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replacing



with



should do the trick.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Aby Ouseph
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 is consider as a primitive datatype. To add 3 to that vector yo have to convert that 3 to Integer Object.
Try this before adding to vector
Integer x=new Integer(3);
then add x
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, use ArrayList instead of Vector.

Vector is a legacy collection class, from Java version 1.0. In version 1.2, new collection classes were added to Java. ArrayList is the replacement for Vector. Although the old collection classes are still in the Java API, there's no real reason anymore to use them in your programs.

See: The Collections Framework.
Originally posted by Aby Ouseph:
3 is consider as a primitive datatype. To add 3 to that vector yo have to convert that 3 to Integer Object.
Try this before adding to vector
Integer x=new Integer(3);
then add x

There's no need for that if you're using Java 5 or 6, because of autoboxing (a new feature in Java 5). This will work without errors in Java 5 or 6:

[ May 31, 2007: Message edited by: Jesper Young ]
 
Saket Barve
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To add 3 to that vector yo have to convert that 3 to Integer Object.


Not required for JDK 1.5+ what with the Autoboxing feature.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is the replacement for Vector. Although the old collection classes are still in the Java API, there's no real reason anymore to use them in your programs.


I thought that Vector was still useful because it is thread-safe and that ArrayList should be used when thread-safety isn't an issue to get higher performance.

Kaydell
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kaydell Leavitt:
I thought that Vector was still useful because it is thread-safe and that ArrayList should be used when thread-safety isn't an issue to get higher performance.

Kaydell

Thread safety is an issue only when you're writing a multi-threaded program and there are multiple threads that are modifying the same collection at the same time.

In the rare case that you really need a synchronized (thread-safe) collection, you could wrap your ArrayList in a thread-safe list like this:
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saket Barve:
Replacing
with
should do the trick.


Then what if I want to add various types of objects (say, Strings, Integers, Panels) to the same vector?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prosenjit Banerjee:
Then what if I want to add various types of objects (say, Strings, Integers, Panels) to the same vector?

Instead of Integer use a common superclass between the objects you want to add. Since String, Integer and Panel have no other common superclass than Object, use:

Ofcourse, since Object is the superclass of all classes, this won't provide much type-safety...

Another example: Suppose you could add different kinds of numbers (integers, longs, doubles, ...). Then you could use Number as the parametric type, since Number is a common superclass of Integer, Long, Double etc.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is the code above the equivalent to:



Kaydell
[ June 21, 2007: Message edited by: Kaydell Leavitt ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!