3

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.

Community
  • 1
  • 1
Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77

2 Answers2

9

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.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • Will upvote if you remove this incorrect information - `The "obsolete type" warning is there because Vector has been deprecated in favor of List`. – Perception Nov 28 '12 at 05:23
  • @Perception: I've changed it to "(essentially) deprecated", because outside of JavaME it *has* been essentially (but not officially) deprecated. Using `Vector` in anything other than a JavaME or legacy system would raise eyebrows at code review time. Netbeans is issuing a warning about this convention, so at least the Netbeans people agree with me :) – Cameron Skinner Nov 28 '12 at 05:26
  • I absolutely agree, its a minor quibble lest the OP actually believe Vector has been officially deprecated. I myself havent used the class in maybe 7 years. +1. – Perception Nov 28 '12 at 05:34
  • @Perception: OK, good call. I can see how that could have been inferred. – Cameron Skinner Nov 28 '12 at 05:35
0

The unsafe warning has nothing to do with Vector in particular. It's just that the code isn't specifying what the Vector is supposed to contain. In general, you want to use ArrayLists instead of Vectors, and you probably also want to specify the type that will go in the container.

The Enumeration interface is out of date as well. You should prefer iterators. In fact there are language features that take advantage of them. e.g. the "new" for loop.

Note: I am making the assumption that J2ME supports these features. If it doesn't then there's nothing wrong with the, "old ways." They're just not optimal.

e.g. (not for J2ME, but for Java 5 or better)

public static void main(String[] args) {
    ArrayList<Integer> vector = new ArrayList<Integer>();
    for(int i=1; i<=10; i++)
        vector.addElement(i);

    for(Integer i : vector)
        System.out.println(i);
}

As per gnat's comments, the above code will not work in J2ME, which is a subset of 1.3. Apparently, even ArrayList is not supported. So the original code in the question's example looks like the way to go.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57