When I am adding a String object into a vector then the following warning occurs.Why?
TestCollectionsMain.java:14: warning: [unchecked] unchecked call to add(E) as a
member of the raw type java.util.Vector
vec.add(“M”);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since Java 1.5 you’re recommended to use the generic’s version of those methods.
If you insist to use a raw type, you may safely ignore the warning.
BTW, you probably should use
ArrayListinstead ofVectorit is a bit faster and does basically the same.This will run, just ignore the warning.
This would be better:
Using generics give you two benefits.
1) Helps you to check at compile time, the values added to the collection are of the same type.
2) Help you to avoid casting when getting the values out of the collection.
But, that’s just an option ( no a compiler error ) if you still want to use the non-generic version, you’re free to do it so, just ignore the exception, or as jskggz says, just add:
To your method.