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
Vectoris a parameterized type. It’s actuallyVector<E>, and you should be providing a type argument when you useVector. If you use a “raw”Vectorthen 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
Vectorhas been (essentially) deprecated in favor ofListand its implementations (ArrayListandLinkedList, among others).The various
Listtypes are the general-purpose replacements forVector.ArrayListcan be used as a more-or-less drop-in replacement forVector, but there are a few differences that you should be aware of. Read the javadocs for more information.The most important difference is that
Vectoris thread-safe butArrayListandLinkedListare not. If you are relying onVectors built-in thread safety then you should take a look at theCollections.synchronizedListmethod.EDIT: Oh, you’re using JavaME. You’re probably stuck with
Vectorin 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.