Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

enumerable - Common Ancestor to Java Array and List

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

I agree it's not ideal though.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...