简体   繁体   English

在java中使用Vector是否安全?

[英]Is it safe to use Vector in java?

I have written following java code: 我写了以下java代码:

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". Netbeans抱怨“过时收藏”的消息。

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. 注意 ,我需要在J2ME应用程序中使用Vector作为存储元素顺序的动态数组。 I would be happy using Hashtable but unfortunately it doesn't store the order of its elements. 我很乐意使用Hashtable但不幸的是它不存储其元素的顺序。

EDIT 1 After reviewing this answer I changed the declaration from Vector vector = new Vector(); 编辑1在查看此答案后,我从Vector vector = new Vector();更改了声明Vector vector = new Vector(); into Vector<String> vector = new Vector<String>(); 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. 您看到的关于“未检查或不安全操作”的警告是因为Vector是参数化类型。 It's actually Vector<E> , and you should be providing a type argument when you use Vector . 它实际上是Vector<E> ,你应该在使用Vector时提供一个类型参数。 If you use a "raw" Vector then you do not get any of the advantages of Java's generics framework. 如果您使用“原始” Vector那么您将无法获得Java的泛型框架的任何优势。 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). “过时类型”警告就在那里,因为Vector已经(基本上)不赞成使用List及其实现( ArrayListLinkedList等)。

The various List types are the general-purpose replacements for Vector . 各种List类型是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. ArrayList可以用作Vector的一个或多或少的替代品,但是你应该注意一些差异。 Read the javadocs for more information. 阅读javadocs以获取更多信息。

The most important difference is that Vector is thread-safe but ArrayList and LinkedList are not. 最重要的区别是Vector是线程安全的,但ArrayListLinkedList不是。 If you are relying on Vector s built-in thread safety then you should take a look at the Collections.synchronizedList method. 如果您依赖于Vector的内置线程安全性,那么您应该查看Collections.synchronizedList方法。

EDIT: Oh, you're using JavaME. 编辑:哦,你正在使用JavaME。 You're probably stuck with Vector in that case. 在这种情况下,你可能会遇到Vector 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. 他们有没有告诉你,如果你不小心有可能是一个问题,但如果细心,你会没事的。

The unsafe warning has nothing to do with Vector in particular. 不安全警告与Vector无关。 It's just that the code isn't specifying what the Vector is supposed to contain. 只是代码没有指定Vector应包含的内容。 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. 通常,您希望使用ArrayLists而不是Vectors,并且您可能还想指定将在容器中的类型。

The Enumeration interface is out of date as well. Enumeration接口也已过时。 You should prefer iterators. 你应该更喜欢迭代器。 In fact there are language features that take advantage of them. 事实上,有一些语言功能可以利用它们。 eg the "new" for loop. 例如循环的“新”。

Note: I am making the assumption that J2ME supports these features. 注意:我假设J2ME支持这些功能。 If it doesn't then there's nothing wrong with the, "old ways." 如果没有,那么“旧方式”就没有错。 They're just not optimal. 他们只是不是最优的。

eg (not for J2ME, but for Java 5 or better) 例如(不适用于J2ME,但适用于Java 5或更高版本)

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. 根据gnat的评论,上述代码在J2ME中不起作用,J2ME是1.3的子集。 Apparently, even ArrayList is not supported. 显然,甚至不支持ArrayList。 So the original code in the question's example looks like the way to go. 所以问题示例中的原始代码看起来就像是要走的路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM