Java Arrays & Generics : Java Equivalent to C# IEnumerable

前端 未结 5 1077
小鲜肉
小鲜肉 2020-12-15 15:12

So in C#, I can treat a string[] as an IEnumerable.

Is there a Java equivalent?

5条回答
  •  旧巷少年郎
    2020-12-15 15:53

    I believe the Java equivalent is Iterable. Although String[] doesn't implement it, you can loop over the elements anyway:

    String[] strings = new String[]{"this", "that"};
    for (String s : strings) {
        // do something
    }
    

    If you really need something that implements Iterable, you can do this:

    String[] strings = new String[]{"this", "that"};
    Iterable stringIterable = Arrays.asList(strings);
    

提交回复
热议问题