为什么会有多余的< E>在这种通用方法中? [英] Why is there an extra <E> in this generic method?

查看:86
本文介绍了为什么会有多余的< E>在这种通用方法中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我学习了Java泛型,但是现在我正在学习集合,并发现了一些我不理解的代码.这是代码:

I learned java generics some time ago, but now I'm learning collections and found some code that I don't understand. Here is the code:

static <E> List<E> nCopies(int n, E value)

它来自类java.util.Collections.

我的问题是为什么会出现:

My question is why there is:

<E> List<E>

不仅

List<E>

很明显我缺少了什么,有人可以帮我澄清一下吗?

Obviously I am missing something, can someone clarify this for me?

推荐答案

<E> List<E>中,第一个<E>表示E type参数.如果未指定,则Java会认为E value中的E引用了名为E的实际类,并要求您将其导入.参见通用方法.

In <E> List<E>, the first <E> denotes that E is a type parameter. If you hadn't specified it, then Java would think the E in E value referred to an actual class named E, and ask you to import it. See generic methods.

这篇关于为什么会有多余的&lt; E&gt;在这种通用方法中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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