Java Arrays & Generics : Java Equivalent to C# IEnumerable<T> Java Arrays & Generics : Java Equivalent to C# IEnumerable<T> arrays arrays

Java Arrays & Generics : Java Equivalent to C# IEnumerable<T>


Iterable<String> is the equivalent of IEnumerable<string>.

It would be an odditity in the type system if arrays implemented Iterable. String[] is an instance of Object[], but Iterable<String> is not an Iterable<Object>. Classes and interfaces cannot multiply implement the same generic interface with different generic arguments.

String[] will work just like an Iterable in the enhanced for loop.

String[] can easily be turned into an Iterable:

Iterable<String> strs = java.util.Arrays.asList(strArray);

Prefer collections over arrays (for non-primitives anyway). Arrays of reference types are a bit odd, and are rarely needed since Java 1.5.


Are you looking for Iterable<String>?

Iterable<T> <=> IEnumerable<T>Iterator<T> <=> IEnumerator<T>