• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Vectors

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic