gpt4 book ai didi

java - Guava 逆源奇数通用语法

转载 作者:行者123 更新时间:2023-12-01 06:35:25 24 4
gpt4 key购买 nike

 /**
* Returns a reversed view of the specified list. For example, {@code
* Lists.reverse(Arrays.asList(1, 2, 3))} returns a list containing {@code 3,
* 2, 1}. The returned list is backed by this list, so changes in the returned
* list are reflected in this list, and vice-versa. The returned list supports
* all of the optional list operations supported by this list.
*
* <p>The returned list is random-access if the specified list is random
* access.
*
* @since Guava release 07
*/

public static <T> List<T> reverse(List<T> list) {
if (list instanceof ReverseList) {
return ((ReverseList<T>) list).getForwardList();
} else if (list instanceof RandomAccess) {
return new RandomAccessReverseList<T>(list);
} else {
return new ReverseList<T>(list);
}

我以前从未见过这种语法:

 public static <T> List<T> reverse(List<T> list)

<T> List<T> 到底是什么?意思是?我以为会是:

 public static List<T> reverse(List<T> list)

最佳答案

第一个<T>显示它是 generic method ,介绍T作为type variable 。不要忘记Lists本身不是泛型类型 - 所以如果它只是

public static List<T> reverse(List<T> list)

...您会期待什么T引用一下?

请参阅Java Generics FAQ entry for generic methods了解更多信息。

关于java - Guava 逆源奇数通用语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com