ArrayList(Collection<? extends E> c) [英] ArrayList(Collection&lt;? extends E&gt; c)

查看:15
本文介绍了ArrayList(Collection<? extends E> c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道为了创建一个 ArrayList 类型的对象,我需要:

I know that in order to create an object of ArrayList type I need to:

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

我知道,因为我在书中看到过.

I know it because I've seen this in books.

但是查看 ArrayList 构造函数的 Java SE API,我看到:ArrayList(Collection c)

But looking at Java SE API for the ArrayList constructor I see: ArrayList(Collection<? extends E> c)

我不明白,我怎么会想到指定我的新 ArrayList 对象将保存的对象类型?我如何从这个定义中知道我必须在实例化和变量声明期间指定 ?

I don't understand, how would I ever get an idea to specify the type of objects my new ArrayList object would hold? How would I know from this definition that I have to specify <MyType> during instantiation and variable declaration?

推荐答案

为了找出你的声明中是否需要类型参数,你不看构造函数,你看类定义本身.构造函数的参数与对象本身的类型无关——构造函数可以接受任何类型的参数,就像任何其他方法一样.这是类定义:

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

class ArrayList 部分说有一个叫做 ArrayList 的类,它需要一个类型参数(这里叫做 E).

The class ArrayList<E> part says that there is a class called ArrayList and it requires one type parameter (which is called E here).

这篇关于ArrayList(Collection<? extends E> c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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