• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Vectors  RSS feed

 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a compiler warning using vectors. I have a class I created and am storing a bunch of those objects in a vector using add. The compiler gives me a warning stating my class uses unchecked or unsafe operations. I looked at the docs and add takes an argument of type E. I can't find what E is. I don't remember having problems using vectors a few years back. Did something change or am I doing something wrong?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E is a type parameter.

In version 1.5 of the JDK, generics are introduced.

The Vector class and others are modified to use type parameters.

If you don't specify the type that the Vector or other Collection holds, then you will get the warning when you compile.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably using JDK 1.5 and that is because of generics, a nice feature incorporated in Collections.

Download a free book on Generics by Maurice Naftalin and Philip Wadler in http://java.net.

Now you can do it like this:



I would like to take this opportunity to tell you that Vector, Stack and Hashtable are considered now legacy collections.

Now we typically use ArrayList, LinkedList, HashMap, and other collections part of the JCF (Java Collections Framework).
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's right.

If you had chosen for Vector because of it's synchronized behaviour, it might be interesting to know that you can create a synchronized ArrayList as follows:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!