简体   繁体   English

的ArrayList(收藏 <? extends E> C)

[英]ArrayList(Collection<? extends E> c)

I know that in order to create an object of ArrayList type I need to: 我知道为了创建ArrayList类型的对象,我需要:

ArrayList<MyType> l = new ArrayList<MyType>();

I know it because I've seen this in books. 我知道,因为我在书中看到了这一点。

But looking at Java SE API for the ArrayList constructor I see: ArrayList(Collection<? extends E> c) 但是看看ArrayList构造函数的Java SE API,我看到: ArrayList(Collection<? extends E> c)

I don't understand, how would I ever get an idea to specify the type of objects my new ArrayList object would hold? 我不明白,我怎么会想到指定我的新ArrayList对象将包含的对象类型? How would I know from this definition that I have to specify <MyType> during instantiation and variable declaration? 我如何从这个定义中知道我必须在实例化和变量声明期间指定<MyType>

That method is meant to copy an existing collection instance to the new ArrayList not to create on from scratch. 该方法旨在将现有集合实例复制到新的ArrayList,而不是从头开始创建。 The elements of the collection it will accept have an upper bound of type E which will be the type of your new ArrayList then. 它将接受的集合的元素具有类型E的上限,然后它将是新ArrayList的类型。

In order to find out if you need type parameters in your declaration, you don't look at the constructor, you look at the class definition itself . 为了找出你的声明中是否需要类型参数,你不要看构造函数,你看一下类定义本身 The arguments of the constructors have nothing to do with the type of the object itself - a constructor can take any kind of arguments, just like any other method. 构造函数的参数与对象本身的类型无关 - 构造函数可以采用任何类型的参数,就像任何其他方法一样。 Here's the class definition: 这是类定义:

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

The class ArrayList<E> part says that there is a class called ArrayList and it requires one type parameter (which is called E here). class ArrayList<E>部分表示有一个名为ArrayList的类,它需要一个类型参数(这里称为E)。

Let's say you have a set of shape classes. 假设你有一组形状类。 The base class is Shape , and the two implementations are Square and Circle . 基类是Shape ,两个实现是SquareCircle Now, after you've done some specific setup for the list of objects you wanted, you need to add it all together to send it to a rendering function. 现在,在对所需对象列表进行了一些特定设置之后,需要将它们全部添加到一起以将其发送到渲染功能。 The code below should make what I'm describing a little more clear: 下面的代码应该使我所描述的内容更加清晰:

ArrayList<Square> squares = readSquares();
ArrayList<Circle> circles = readCircles();

ArrayList<Shape> queue= new ArrayList<Shape>(squares);
queue.addAll(circles);

renderShapes(queue);

The ArrayList(Collection<? extends E> c) constructor makes line five possible. ArrayList(Collection<? extends E> c)构造函数使第五行成为可能。 With generics, the processor is not smart enough to automatically determine that Square extends Shape by default. 对于泛型,处理器不够智能,无法自动确定Square默认扩展Shape。 You have to tell the compiler that is what you intended. 你必须告诉编译器你的意图。 The way you do that is with the wildcard <? extends E> 你这样做的方法是使用通配符<? extends E> <? extends E> . <? extends E> Now the compiler will make sure that every object added is at least a Shape. 现在编译器将确保添加的每个对象至少是一个Shape。

That's always true when you construct an object for a class that takes a generic type. 当你为一个采用泛型类的类构造一个对象时,这总是正确的。 If you scroll up to the top, you'll see Class ArrayList< E >. 如果向上滚动到顶部,您将看到Class ArrayList <E>。 That's the hint. 这是暗示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 的ArrayList(收藏 <? extends E> c)线程安全吗? - ArrayList(Collection<? extends E> c) thread safe? ArrayList上的MergeSort仅与ArrayList(Collection <? extends E> c)不是ArrayList(int initialCapacity)吗? - MergeSort on ArrayList only work with ArrayList(Collection<? extends E> c) not ArrayList(int initialCapacity)? 当使用 ArrayList(Collection<? extends E> c) 对于复制构造函数,我收到两个错误 - When using ArrayList(Collection<? extends E> c) for a copy constructor I get two errors 如何重写Java的ArrayList的addAll(Collection <? extends E> c)Scala中的方法? 天真的方法不能编译 - How to override Java's ArrayList's addAll(Collection<? extends E> c) method in Scala? Naive approach does not compile 这到底是什么意思收藏<? extends E> C - What exactly does this mean Collection<? extends E> c Java,如何遍历集合 <? extends E> ? - Java, How Iterate through Collection<? extends E>? 是我的空“公共课 xList<E> 扩展数组列表<E> “ 具体的? - Is my empty "public class xList<E> extends ArrayList<E>" concrete? 扩展ArrayList的Serializable类的writeObject和readObject方法<E> - writeObject and readObject method for a Serializable class which extends ArrayList<E> C#相当于Java类 <E> 和E扩展枚举 <E> - C# equivalent of java Class<E> and E extends Enum<E> Collection类型不是通用的;它不能用参数<?参数化延伸E> - The type Collection is not generic; it cannot be parameterized with arguments <? extends E>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM