ArrayList的(集&LT ;?扩展E> C) [英] ArrayList(Collection<? extends E> c)

查看:169
本文介绍了ArrayList的(集&LT ;?扩展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.

但看的Java SE API的ArrayList的构造我看到:
的ArrayList(收集和LT ;?扩展E&GT; C)

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

我不明白,怎么我会永远得到一个想法,以指定的对象我的新ArrayList对象将持有的类型? MyType的&GT;我将如何从这个定义,我必须指定&LT知道吗?实例化和变量声明中

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

类的ArrayList&LT; E&GT; 部分说,有一个名为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的(集&LT ;?扩展E&GT; C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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