views:

162

answers:

5
+4  Q: 

Iterator for array

How to get an iterator for an array in java?

int[] arr={1,2,3};
for(int i:arr)
 System.out.println(i);

How does the above for-each loop work ? Is the array converted to a list to get the iterator ?

+12  A: 

No, there is no conversion. The JVM just iterates over the array using an index in the background.

Quote from Effective Java 2nd Ed., Item 46:

Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once.

So you can't get an Iterator for an array (unless of course by converting it to a List first).

Péter Török
ok how can i get an iterator for an array?
Emil
Yes, +1 It's prolly just a regular `for`. @Emil, you can't.
st0le
+6  A: 
Arrays.asList(arr).iterator();

Or write your own, implementing ListIterator interface..

Jochem
`Arrays.asList()` can not take an `int[]`, only `Object[]` and subclasses.
ILMTitan
+2  A: 

You can't directly get an iterator for an array.

But you can use a List, backed by your array, and get an ierator on this list. For that, your array must be an Integer array (instead of an int array):

Integer[] arr={1,2,3};
List<Integer> arrAsList = Arrays.asList(arr);
Iterator<Integer> iter = arrAsList.iterator();

Note: it is only theory. You can get an iterator like this, but I discourage you to do so. Performances are not good compared to a direct iteration on the array with the "extended for syntax".

Note 2: a list construct with this method doesn't support all methods (since the list is backed by the array which have a fixed size). For example, "remove" method of your iterator will result in an exception.

Benoit Courtine
I don't think it will work.Arrays.asList(arr) will return List of type int[].Since it is primitive array.
Emil
@Emil: Java generics don’t work for primitive types. `Arrays.asList` implicitly takes care of this by boxing the values in the array. Hence, the result really *is* a `List<Integer>`.
Konrad Rudolph
@emil it works because Arrays.asList takes a varargs parameter
seanizer
@seanizer:but i tried the above code in my IDE and it shows error.
Emil
it compiles in my eclipse. have you set the compiler compliance to at least 1.5 ?
seanizer
+1  A: 

Strictly speakintg, you can't get an iterator of the primitive array, because Iterator.next() can only return an object. But through the magic of autoboxing, you can get the iterator using a list with Arrays.asList()

Iterator<Integer> it = Arrays.asList(arr).iterator();
seanizer
A: 

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

If you search for lang:java ArrayIterator in Google Code search, you'll find lots more.

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[].

uckelman
Why the downvote? At least indicate your point of disagreement. What I say here addresses the first question asked, namely, how to get an `Iterator` for an array.
uckelman