• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generics related warning in Vector add method

 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am creating a Vector instance.
And adding elements like vectorInstance.add("myData");

To the add method I am getting warning in my IDE (Eclipse) as following-
Type safety: The method add(Object) belongs to the raw type Vector. References to generic type Vector<E> should be parameterized

I am using jdk1.6. All I know is this is generics related warning.
What would be a good programming practice in this case?

Can you give me example? And where to check in case I get such warnings for any other classes in java?

Thank you!

Regards,
Leena
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lesson: Generics

In your case, declare and initialize your vector as this:
As a side note, don't declare vectorInstance as a Vector, but as a List:
This is called programming against interfaces; do a search around this forum to find out why this is better (the question has been asked many, many times).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another point: Is there a specific reason that you are using class Vector instead of class ArrayList?

Class Vector is one of the old legacy collection classes from Java 1.0 / 1.1. In Java 1.2 (long ago...) a new collections framework was introduced, including ArrayList to replace Vector (there are small differences, but those are not relevant for most situations). Prefer the newer classes such as ArrayList above the old ones such as Vector.

Also see the Collections trail in Sun's Java Tutorials.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic