List> 是什么意思? java中的泛型是什么意思?
List
是什么意思,它是否只是表示未指定类型的对象列表?
谷歌搜索字符串 没有返回任何有用的结果(:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
List
是什么意思,它是否只是表示未指定类型的对象列表?
谷歌搜索字符串 没有返回任何有用的结果(:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
正如 Tom 所说,
?
,或无界通配符,意味着未指定对象的类型。它可能是未知的,可能意味着多个可能的值,或者可能完全不相关。您的示例List
发音为“List ofknown”。它很方便,因为它很灵活,但也有一些陷阱,因为你不能将随机对象放入未知组中并将它们从未知组中拉出而完全不受惩罚。资源:
顺便说一句,您的 Google 搜索失败了,因为 Google 不使用特殊字符:
-Google 帮助页面
(编辑:我一定是当我昨晚写这篇文章时真的很累。)
As Tom said, the
?
, or unbounded wildcard, means that the type of the object is not specified. It could be unknown, could be meant for multiple possible values or might be just plain irrelevant. Your example,List<?>
, is pronounced "List of unknown." It's convenient because it's flexible, but there are also some pitfalls because you can't shove random objects in and pull them out of groups of unknown with total impunity.Resources:
Incidentally, your Google search failed because Google doesn't truck with special characters:
-Google help page
(EDIT: I must have been really tired when I wrote this last night. Cleaned up formatting/added a little info.)
您需要获取更多信息的关键字是 通配符
The keyword you need to get more information is Wildcards
为了回答这个问题,我需要解释一下无界通配符和有界通配符。
这篇文章的内容是根据 java 文档汇编而成的。
1.无限通配符
2. 有界通配符
类似地,语法
? super T
是有界通配符,表示未知类型,是 T 的超类型。ArrayedHeap280<?例如,super Integer>包括
ArrayedHeap280
、ArrayedHeap280
和ArrayedHeap280
To answer this question I need to explain Unbounded Wildcards and Bounded Wildcards.
The content of this post has been assembled from java documentation.
1. Unbounded Wildcards
2. Bounded Wildcards
Similarly, the syntax
? super T
, which is a bounded wildcard, denotes an unknown type that is a supertype of T.A
ArrayedHeap280<? super Integer>
, for example, includesArrayedHeap280<Integer>
,ArrayedHeap280<Number>
, andArrayedHeap280<Object>
.As you can see in the java documentation for Integer class, Integer is a subclass of Number that in turn is a subclass of Object.
List
:完全没有类型限制和赋值限制。List
List
:它是一个泛型类型。赋值前,意味着它可以接受任何类型的集合赋值,但赋值后,你不能向其中添加
元素,但可以删除
和明确
,而不是不可变集
。List
一般用作参数来接收外部集合,或者返回特定元素类型的集合,也称为通配符集合
。测试代码及结果如下:
结果为:
List
: There is no type restriction and assignment restriction at all.List<Object>
: It seems to be used the same asList
, but a compilation error will occur when accepting other generic assignments.List<?>
: It is a generic type. Before assignment, it means that it can accept any type of set assignment, but after assignment, you can'tadd
elements to it, but you canremove
andclear
, not animmutable set
.List<?>
is generally used as a parameter to receive an external collection, or return a collection of specific element types, also known as awildcard collection
.The test code and result as followed:
The result is :
听起来您应该寻找一些有关 Java 泛型的文档。
List
表示它是基于当前未指定类型的对象。该规范是在类实例化时制定的。例如:
是一个 String 对象的列表。
Sounds like you should look for some documentation on Java generics.
The
List<?>
means that it is an object based on a currently unspecified type. That specification is made when the class is instantiated.For example:
is a list of String objects.
List
代表List
所以在Collection
中你会发现containsAll(Collection c)
它允许你写List<?>
stands forList<? extends Object>
so inCollection<E>
you will findcontainsAll(Collection<?> c)
which allows you to writeList
是一个您可以自己实现的接口,也可以由一些 Java 集合实现,例如Vector
。您可以使用尖括号提供编译时类型信息。最通用的类型是
Object
,即List
您无法实例化
List
;它是一个接口,而不是一个实现。List
is an interface you can implement yourself and also implemented by some of the Java collections, likeVector
.You can provide compile-time typing information using the angled brackets. The most generic type would be
Object
, which would beList<Object>
. The<?>
you see is indicating a List of some subclass ofObject
or anObject
. This is like sayingList<? extends Object>
, orList<? extends Foo>
, where theList
contains objects of some subclass ofFoo
or objects ofFoo
itself.You can't instantiate a
List
; it's an interface, not an implementation.当您从 Collection 中取出一个元素时,必须将其转换为存储在集合中的元素类型。这除了不方便之外,也不安全。编译器不会检查您的转换是否与集合的类型相同,因此转换可能在运行时失败。
泛型为您提供了一种将集合类型传达给编译器的方法,以便可以对其进行检查。一旦编译器知道集合的元素类型,编译器就可以检查您是否一致地使用了该集合,并且可以对从集合中取出的值插入正确的转换。
chk pdf
When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.
Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.
chk dis pdf
?只不过是泛型中的通配符
泛型中有 3 种不同类型的通配符
1) 上界通配符:使用扩展关键字
2) 下界通配符
3) 无界通配符
? is nothing but Wildcard in Generics
There are 3 different kind of Wildcards in Generics
1) Upper Bounded Wildcards: Uses extends key word
2) Lower Bounded Wildcards
3) Unbounded Wildcard
列表相当于列表
Java 规范中的泛型
List<?> is equivalent to List<? extends Object>
<?> in java specification
Generics in java specification
您可能正在查看基于模板的
List
类。您可以通过List 创建字符串列表myList = new MyList();
作为示例。检查文档以了解它支持的所有类型。它应该支持任何对象类型,但如果有排序功能,则必须提供一些比较函数。请注意,在上面的示例中,
MyList
是一个在 Java 中实现List
接口的具体类。它可以是ArrayList。编辑:
我错误地将
List
假设为具体类。修正了上面的错误。谢谢乔恩。You are probably looking at the template based
List
class. You can create a list of strings byList<String> myList = new MyList<String>();
as an example. Check the documentation for all the types it supports. It should support any object type, but if there is a sort functionality you have to supply some compare functions.Note that in the example above
MyList
is a concrete class that implements theList
interface in Java. It can beArrayList
.EDIT:
I assumed
List
as a concrete class by mistake. Fixed the error above. Thanks Jon.