I have written following java code:
public static void main(String[] args) {
Vector vector = new Vector();
for(int i=1; i<=10; i++)
vector.addElement(i);
Enumeration vEnum = vector.elements();
while(vEnum.hasMoreElements())
System.out.println(vEnum.nextElement());
}
While compiling it getting following warning message:
Note: TestJavaApplication.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
And Netbeans complaining with a message of "Obsolete Collection".
What do you recommend me in this situation?
Note, that I need to use Vector in J2ME application as a dynamic array that stores the order of the elements. I would be happy using Hashtable but unfortunately it doesn't store the order of its elements.
EDIT 1
After reviewing this answer I changed the declaration from Vector vector = new Vector(); into Vector<String> vector = new Vector<String>();. And now getting another warning message:
TestJavaApplication.java:2: warning: com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration is Sun proprietary API and may be removed in a future release
import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;
^
Thank you.
The warning you are seeing about "unchecked or unsafe operations" is because Vector is a parameterized type. It's actually Vector<E>, and you should be providing a type argument when you use Vector. If you use a "raw" Vector then you do not get any of the advantages of Java's generics framework. The "unsafe" warning means that you are missing out on some type safety.
The "obsolete type" warning is there because Vector has been (essentially) deprecated in favor of List and its implementations (ArrayList and LinkedList, among others).
The various List types are the general-purpose replacements for Vector. ArrayList can be used as a more-or-less drop-in replacement for Vector, but there are a few differences that you should be aware of. Read the javadocs for more information.
The most important difference is that Vector is thread-safe but ArrayList and LinkedList are not. If you are relying on Vectors built-in thread safety then you should take a look at the Collections.synchronizedList method.
EDIT: Oh, you're using JavaME. You're probably stuck with Vector in that case. Nevertheless, the generic type warning still applies.
You can ignore the warnings if you wish. They're there to tell you that there might be a problem if you're not careful, but if you are careful then you'll be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With