Java数组和列表的共同祖先 [英] Common Ancestor to Java Array and List

查看:209
本文介绍了Java数组和列表的共同祖先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,数组和列表都具有Enumerable作为祖先,因此接受Enumerable作为参数的方法可以接收数组和列表作为其参数。
我想知道Java中是否有类似的东西?

In .NET, both array and list have Enumerable as ancestor, so a method that accept Enumerable as an argument can receive both array and list as its argument. I wonder if there is a similar thing in Java?

推荐答案

不,Java中没有相应的东西。我通常建议你设计API方法来接收 List< T> Collection< T> 可迭代< T> 。虽然这些阻止直接使用数组调用方法,但您可以使用 Arrays.asList 非常容易地包装数组。对于调用者来说,这比将数组指定为方法参数更加灵活,这会强制执行单个实现。

No, there's no equivalent in Java. I would generally suggest that you design API methods to receive List<T>, Collection<T> or Iterable<T>. While these preclude directly calling the method with an array, you can wrap an array very easily using Arrays.asList. This is more flexible for the caller than specifying an array as a method parameter, which forces a single implementation.

我同意它并不理想。

请注意,在.NET中,一维数组不只是实现 IEnumerable< T> - 它们实现 IList< T>

Note that in .NET, single-dimensional arrays don't just implement IEnumerable<T> - they implement IList<T> as well.

这篇关于Java数组和列表的共同祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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