java capture,Java泛型:compareTo和“capture#1-of ?”

The following gives me an error message:

下面給出一個錯誤消息:

public static List> merge(Set>> lists) {

List> result = new LinkedList>();

HashBiMap>, Integer> location = HashBiMap.create();

int totalSize;

for (List> l : lists) {

location.put(l, 0);

totalSize += l.size();

}

boolean first;

List> lowest; //the list with the lowest item to add

int index;

while (result.size() < totalSize) {

first = true;

for (List> l : lists) {

if (! l.isEmpty()) {

if (first) {

lowest = l;

}

else if (l.get(location.get(l)).compareTo(lowest.get(location.get(lowest))) <= 0) { //error here

lowest = l;

}

}

}

index = location.get(lowest);

result.add(lowest.get(index));

lowest.remove(index);

}

return result;

}

The error is:

錯誤的是:

The method compareTo(capture#1-of ?) in the type Comparable is not applicable for the arguments (Comparable)

What's going on here? I made the type of everything Comparable so I could call .compareTo and sort this list. Am I using generics incorrectly?

這是怎么回事?我做了所有可以比較的東西,所以我可以調用。compareto並排序這個列表。我用的是泛型嗎?

1 个解决方案

#1

20

List> means "List of anything", so two objects with this type are not the same: One could be a list of String, the other a list of BigDecimal. Obviously, those are not the same.

列表< ?>的意思是“任何東西的列表”,所以這兩種類型的對象不一樣:一個可以是字符串列表,另一個是BigDecimal列表。顯然,它們是不一樣的。

List means "List of anything but when you see T again, it's the same T".

List

表示“任何東西的列表,但是當你再次看到T時,它是相同的T”。

You must tell the compiler when you mean the same type in different places. Try:

當您在不同的地方表示相同類型時,您必須告訴編譯器。試一試:

public static > List merge(Set> lists) {

List result = new LinkedList();

HashBiMap, Integer> location = HashBiMap.create();

[EDIT] So what does > List mean? The first part defines a type T with the following properties: It must implement the interface Comparable super T> (or Comparable where X is also defined in terms of T).

[編輯]那么,

> >列表的意思嗎?第一部分定義了具有以下屬性的類型T:它必須實現可比較的接口

(或類似的

,其中X也定義為T)。

? super T means that the type which the Comparable supports must T or one of its super types.

嗎?super T表示類似支持的類型必須是T或它的超級類型之一。

Imagine for a moment this inheritance: Double extends Integer extends Number. This is not correct in Java but imagine that Double is just an Integer plus a fraction part. In this scenario, a Comparable which works for Number also works for Integer and Double since both derive from Number. So Comparable would satisfy the super part for T being Number, Integer or Double.

想象一下這個繼承:雙擴展整數擴展數。這在Java中是不正確的,但請想象一下,Double只是一個整數加上一個小數部分。在這個場景中,一個可比較的數字也適用於整數和Double,因為它們都來自於數字。因此,可以比較的 將滿足T為數字、整數或Double的超級部分。

As long as each of these types support the Comparable interface, they also satisfy the first part of the declaration. This means, you can pass in Number for T and the resulting code will also work when there are Integer and Double instances in the lists. If you Integer for T, you can still use Double but Number is not possible because it doesn't satisfy T extends Comparable anymore (the super part would still work, though).

只要這些類型支持可比較的接口,它們也滿足聲明的第一部分。這意味着,當列表中有整數和雙實例時,您可以傳入T的數字,結果代碼也會工作。如果你用整數表示T,你仍然可以使用Double,但是Number是不可能的,因為它不滿足T擴展的可比性(盡管超級部分仍然有效)。

The next step is to understand that the expression between static and List just declares the properties of the type T which is used later in the code. This way, you don't have to repeat this long declaration over and over again. It's part of the behavior of the method (like public) and not part of the actual code.

下一步是要了解靜態和列表之間的表達式只是聲明了在代碼中稍后使用的類型T的屬性。這樣,你就不必一次又一次地重復這個冗長的聲明。它是方法行為的一部分(如public),而不是實際代碼的一部分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值