(Java) 迭代 Vector,为什么 .next() 是一个对象,而不是 String[]?

发布于 2025-01-06 10:26:16 字数 561 浏览 2 评论 0原文

在 Java 中,当您对 Vector 进行迭代时,为什么 .next() 是需要转换的 ObjectString[],将每个元素用作 String[]

编辑:

这是我的代码:

    Iterator itr = getIdAndName().iterator();
    while( itr.hasNext() ) {
        String[] stringArray = (String[])itr.next();
        String id = stringArray[0];
        String name = stringArray[1];
        System.out.println(id + ": " + name);
    }

getIdAndName() 返回Vector

In Java, when you iterator over a Vector<String[]>, why is .next() an Object that needs to be casted to String[], to use each element as a String[]?

EDIT:

Here is my code:

    Iterator itr = getIdAndName().iterator();
    while( itr.hasNext() ) {
        String[] stringArray = (String[])itr.next();
        String id = stringArray[0];
        String name = stringArray[1];
        System.out.println(id + ": " + name);
    }

getIdAndName() returns Vector<String[]>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离不开的别离 2025-01-13 10:26:16

事实并非如此。我唯一能想到的是你没有输入迭代器,即你正在这样做:

Vector<String[]> vector;
Iterator it = vector.iterator();
Object obj = it.next();

当你应该这样做时:

Vector<String[]> vector;
Iterator<String[]> it = vector.iterator();
String[] next = it.next();

好吧,在大多数情况下,你实际上并不直接直接需要迭代器,所以你可以使用:

Vector<String[]> vector;
for (String[] element : vector) {
    //...
}

It isn't. The only thing I can think of is you're not typing your iterator, i.e. you're doing this:

Vector<String[]> vector;
Iterator it = vector.iterator();
Object obj = it.next();

when you should be doing:

Vector<String[]> vector;
Iterator<String[]> it = vector.iterator();
String[] next = it.next();

Well, in most cases you don't actually need the iterator directly, so you could just use:

Vector<String[]> vector;
for (String[] element : vector) {
    //...
}
晨光如昨 2025-01-13 10:26:16

当对声明类型为 Vector 的变量调用时,iterator() 方法会返回 Iterator >。

我怀疑您在声明为 VectorVector 或其他内容的变量上调用它。或者,您可能将迭代器分配给 IteratorIterator 变量,而不是 Iterator。显然,这只是猜测,因为你没有向我们展示源代码。

(请注意,确定是否需要强制转换的是变量声明类型......而不是实例的实际类型。)

The iterator() method returns an Iterator<String[]> when called on a variable whose declared type is Vector<String[]>.

I suspect that you are calling it on a variable that is declared as Vector or Vector<?> or something else. Or maybe you are assigning the iterator to an Iterator or Iterator<?> variable instead of an Iterator<String[]>. Obviously, this is just conjecture, because you didn't show us the source code.

(Note that it is the declared type of the variable that determines whether a cast is required ... not the actual type of the instance.)

夏天碎花小短裙 2025-01-13 10:26:16

当您从向量中获取迭代器时,例如 vector.iterator(); 您的迭代器应该已参数化。这样,迭代器就会知道它存储的对象是 String[] 类型,如果你不告诉迭代器它存储的类型,它将不得不求助于该对象。

当您正确参数化类型时,例如 Iteratoriter = vector.iterator(); ,然后 iter 上的迭代将不需要显式转换。

有关通用,请参阅此处。

When you get iterator from your vector, example vector.iterator(); your iterator should have parameterized. This way, the iterator will know the object that it stored is of type String[], if you do not tell iterator of the type it stored, it will have to resort to the object.

When you properly parameterized the type, for example Iterator<String[]> iter = vector.iterator(); , and then the iteration over iter will not need to be cast explicitly.

Please refer here about generic.

陌上青苔 2025-01-13 10:26:16

当您迭代 Vector 时,您正在使用 Iterator 接口的实现。根据定义,Iterator 接口在调用其 next() 方法时返回一个 Object。您可以在这里看到这个定义 -

http:// docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html

所以无论你迭代什么并不重要,当你调用它的 next 时,迭代器总是返回一个对象() 方法。

顺便说一句,.next() 不必转换为 String[]。在您的特定情况下,Vector 包含 String[] 作为其元素,因此它们被转换为 String[] 以便使用它们。

When you iterate over a Vector, you are using an implementation of the Iterator interface. By DEFINITION, the Iterator interface returns an Object when its next() method is called. You can see this definition here -

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html

So it doesn't matter what you iterate over, the Iterator will always return an Object when you call its next() method.

And by the way, .next() does not HAVE to be casted to a String[]. It is just the case that in your specific case the Vector contains String[] as its elements, and therefore they are casted to String[] in order to use them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文