Java数组和放大器;泛型:相当于Java到C#的IEnumerable< T> [英] Java Arrays & Generics : Java Equivalent to C# IEnumerable<T>

查看:751
本文介绍了Java数组和放大器;泛型:相当于Java到C#的IEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,在C#中,我可以把一个的String [] 的IEnumerable<字符串方式>

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

是否有相当于Java?

Is there a Java equivalent?

推荐答案

可迭代&LT;弦乐&GT; 的IEnumerable&LT相当于;串&GT;

这将是类型系统的odditity如果阵列实施可迭代的String [] 对象[] ,但可迭代℃的实例;串&GT; 不是可迭代&LT;对象&gt; 。类和接口无法实现乘用不同的通用参数相同的通用接口。

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 [] 将在增强的for循环的工作就像一个可迭代

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

的String [] 可以很容易地变成了一个可迭代

String[] can easily be turned into an Iterable:

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

在数组preFER集合(非原语反正)。引用类型的数组是有点奇怪,既然Java 1.5中是很少用到。

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

这篇关于Java数组和放大器;泛型:相当于Java到C#的IEnumerable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆