如何增强为说明书工作阵列,以及如何得到一个数组的迭代器? [英] How does the enhanced for statment work for arrays, and how to get an iterator for an array?
问题描述
由于以下code片断:
INT [] =改编{1,2,3};
对于(INT I:ARR)
的System.out.println(ⅰ);
我有以下问题:
- 如何上面for-each循环工作?
- 如何得到一个迭代器Java中的数组?
- 被转换成一个列表来获得迭代器阵列?
如果你想要一个的Iterator
一个数组,你可以使用直接实现之一在那里,而不是包装的在列表
的数组。例如:
<一个href=\"https://commons.apache.org/proper/commons-collections/javadocs/api-2.1.1/org/apache/commons/collections/iterators/ArrayIterator.html\">Apache共享集合 ArrayIterator
或者,这一个,如果你想使用泛型:
<一个href=\"http://ostermiller.org/utils/ArrayIterator.java.html\"><$c$c>com.Ostermiller.util.ArrayIterator$c$c>
请注意,如果你想有一个的Iterator
过基本类型,你不能,因为原始类型不能是泛型参数。例如,如果你想要一个的Iterator&LT; INT&GT;
,你必须使用一个的Iterator&LT;整数GT;
来代替,这将导致大量的自动装箱的,如果这是由支持-unboxing INT []
。
Given the following code snippet:
int[] arr = {1, 2, 3};
for (int i : arr)
System.out.println(i);
I have the following questions:
- How does the above for-each loop work?
- How do I get an iterator for an array in Java?
- Is the array converted to a list to get the iterator?
If you want an Iterator
over an array, you could use one of the direct implementations out there instead of wrapping the array in a List
. For example:
Apache Commons Collections ArrayIterator
Or, this one, if you'd like to use generics:
com.Ostermiller.util.ArrayIterator
Note that if you want to have an Iterator
over primitive types, you can't, because a primitive type can't be a generic parameter. E.g., if you want an Iterator<int>
, you have to use an Iterator<Integer>
instead, which will result in a lot of autoboxing and -unboxing if that's backed by an int[]
.
这篇关于如何增强为说明书工作阵列,以及如何得到一个数组的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!