(Java) 迭代 Vector,为什么 .next() 是一个对象,而不是 String[]?
在 Java 中,当您对 Vector
进行迭代时,为什么 .next()
是需要转换的 Object
到 String[]
,将每个元素用作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实并非如此。我唯一能想到的是你没有输入迭代器,即你正在这样做:
当你应该这样做时:
好吧,在大多数情况下,你实际上并不直接直接需要迭代器,所以你可以使用:
It isn't. The only thing I can think of is you're not typing your iterator, i.e. you're doing this:
when you should be doing:
Well, in most cases you don't actually need the iterator directly, so you could just use:
当对声明类型为
Vector
的变量调用时,iterator()
方法会返回Iterator
>。我怀疑您在声明为
Vector
或Vector
或其他内容的变量上调用它。或者,您可能将迭代器分配给Iterator
或Iterator
变量,而不是Iterator
。显然,这只是猜测,因为你没有向我们展示源代码。(请注意,确定是否需要强制转换的是变量的声明类型......而不是实例的实际类型。)
The
iterator()
method returns anIterator<String[]>
when called on a variable whose declared type isVector<String[]>
.I suspect that you are calling it on a variable that is declared as
Vector
orVector<?>
or something else. Or maybe you are assigning the iterator to anIterator
orIterator<?>
variable instead of anIterator<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.)
当您从向量中获取迭代器时,例如
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.
当您迭代 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.