Java 数组和列表的共同祖先
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2442337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Common Ancestor to Java Array and List
提问by Ph??ng Nguy?n
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?
在 .NET 中,数组和列表都具有 Enumerable 作为祖先,因此接受 Enumerable 作为参数的方法可以同时接收数组和列表作为其参数。我想知道Java中是否有类似的东西?
回答by Jon Skeet
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 directlycalling 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.
不,Java 中没有等价物。我通常建议您设计 API 方法来接收List<T>,Collection<T>或Iterable<T>. 虽然这些排除了直接使用数组调用方法,但您可以使用Arrays.asList. 这对于调用者来说比将数组指定为方法参数更灵活,后者强制执行单个实现。
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.
请注意,在 .NET 中,一维数组不仅仅实现IEnumerable<T>- 它们也实现IList<T>。
回答by Nate
They don't have a common ancestor, however, there are methods to cast between the two types as needed -
它们没有共同的祖先,但是,有一些方法可以根据需要在两种类型之间进行转换 -
So you could provide an overloaded method to cast to a common type - i.e.
所以你可以提供一个重载的方法来转换为一个常见的类型 - 即
public void doAll(MyType[] array) {
doAll(Arrays.asList(array));
}
public void doAll(List<MyType> list) {
//... process List here.
}
回答by duffymo
Array and List in Java do not share a common ancestor other than java.lang.Object.
Java 中的数组和列表除了 java.lang.Object 之外没有共同的祖先。
Both can be accessed using the foreach loop, like so:
两者都可以使用 foreach 循环访问,如下所示:
String [] array = new String [] { "foo", "bar", "baz", };
List<String> list = Arrays.asList( "x", "y", "z");
for (String s : array)
System.out.println(s);
for (String s : list)
System.out.println(s);
回答by ewernli
Basically, arrays have an implicit type that is a subclass of object. See Arraysin the JLS:
基本上,数组有一个隐式类型,它是对象的子类。请参阅JLS 中的数组:
public static void main(String[] args) {
int[] ia = new int[3];
System.out.println(ia.getClass());
System.out.println(ia.getClass().getSuperclass());
}
> class [I
> class java.lang.Object
The way arrays and lists are handled is also not the same when we consider covariance/contravariance.
当我们考虑covariance/contravariance时,处理数组和列表的方式也不同。
List<Object> l = new ArrayList<String>(); // complain
Object[] l2 = new String[1]; // ok
l2[0] = 4; // throw ArrayStoreException.
It gets even worse if we consider generics, but that's another topic. All in all, I don't know the rationale of this design, but we need to live with it.
如果我们考虑泛型,情况会更糟,但这是另一个话题。总而言之,我不知道这种设计的基本原理,但我们需要接受它。
回答by Cuddlefluff
Iterable<T>is the Java equivalent of IEnumerable<T>. All/most collections implement this interface (including ArrayListand arrays), so yes. But it's not an "ancestor" (which it's not in .NET either), but a common interface.
Iterable<T>是 Java 的等价物IEnumerable<T>。所有/大多数集合都实现了这个接口(包括ArrayList和数组),所以是的。但它不是“祖先”(也不在 .NET 中),而是一个通用接口。
回答by Brian Agnew
Both derive from java.lang.Object. However, this isn't collection-related, which I think is what you're looking for.
两者都源自java.lang.Object. 但是,这与收藏无关,我认为这正是您要寻找的。

