- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
merge(Set>> lists) { List> result = new LinkedList>(); HashB-6ren">
下面给我一条错误信息:
public static List<Comparable<?>> merge(Set<List<Comparable<?>>> lists) {
List<Comparable<?>> result = new LinkedList<Comparable<?>>();
HashBiMap<List<Comparable<?>>, Integer> location = HashBiMap.create();
int totalSize;
for (List<Comparable<?>> l : lists) {
location.put(l, 0);
totalSize += l.size();
}
boolean first;
List<Comparable<?>> lowest; //the list with the lowest item to add
int index;
while (result.size() < totalSize) {
first = true;
for (List<Comparable<?>> 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 method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments (Comparable<capture#2-of ?>)
这是怎么回事?我将所有内容都设为 Comparable
,这样我就可以调用 .compareTo
并对这个列表进行排序。我是否错误地使用了泛型?
最佳答案
List<?>
意思是“任何东西的列表”,所以这个类型的两个对象是不一样的:一个可能是 String
的列表, 另一个列表 BigDecimal
.显然,它们并不相同。
List<T>
意思是“任何东西的列表,但当你再次看到 T
时,它是相同的 T
”。
当你在不同的地方指的是同一类型时,你必须告诉编译器。尝试:
public static <T extends Comparable<? super T>> List<T> merge(Set<List<T>> lists) {
List<T> result = new LinkedList<T>();
HashBiMap<List<T>, Integer> location = HashBiMap.create();
[编辑] 那么 <T extends Comparable<? super T>> List<T>
是什么意思?意思是?第一部分定义了一个类型 T
具有以下属性:它必须实现接口(interface) Comparable<? super T>
(或 Comparable<X>
,其中 X
也是根据 T
定义的)。
? super T
表示 Comparable
的类型支持必T
或其父类(super class)型之一。
想象一下这个继承:Double extends Integer extends Number
.这在 Java 中是不正确的,但想象一下 Double
只是一个 Integer
加上小数部分。在这种情况下,一个 Comparable
适用于 Number
也适用于 Integer
和 Double
因为两者都来自 Number
.所以Comparable<Number>
会满足 super
部分 T
正在Number
, Integer
或 Double
.
只要这些类型中的每一个都支持 Comparable
接口(interface),它们也满足声明的第一部分。这意味着,您可以传入 Number
对于 T
当有 Integer
时,生成的代码也可以工作和 Double
列表中的实例。如果你Integer
对于 T
, 你仍然可以使用 Double
但是Number
不可能,因为它不满足 T extends Comparable
不再(尽管 super
部分仍然有效)。
下一步是了解 static
之间的表达式和 List
只是声明类型的属性 T
稍后在代码中使用。这样,您就不必一遍又一遍地重复这个冗长的声明。它是方法行为的一部分(如 public
),而不是实际代码的一部分。
关于Java 泛型 : compareTo and "capture#1-of ?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1770972/
我一直在阅读 Captures这一段引起了我的兴趣: Inside a Signature, a Capture may be created by prefixing a sigilless par
我在 Java 中使用这个正则表达式: ^(Mon(?:.?|day)?)(?:[\.,])?$ (可以测试 here ) 我想捕获日期,后跟可选的 . 或 ,。如果是星期一,我想捕获 Monday
我正在 try catch 功能键 F1 到 F12 和 4 个箭头键以及主页、插入、删除、结束、向上翻页和向下翻页键。如何???? private void Form1_KeyPress(objec
没有capture="camera" input type="file" 的属性标签 in official w3.org documentation . 讽刺的是,我发现了这么多地方 capture
摘自Huon Wilson的Finding Closure in Rust: Capturing entirely by value is also strictly more general tha
所以我想这样做: public interface IFieldObject { public Comparable get(); } public interface IFieldCondi
我希望使用正则表达式将单词分成组(vowels, not_vowels, more_vowels),使用标记来确保每个单词以元音开头和结尾。 import re MARKER = "~" VOWELS
我在浏览 StackOverflow 时发现了 Szimek/Signature_Pad 以使用 Javascript 捕获电子/数字签名。 我研究过,但我仍然对如何将 DATA URI 捕获到变量中
我正在尝试关注 this example使用带有 remove_if 的 lambda。这是我的尝试: int flagId = _ChildToRemove->getId(); auto new_e
我无法捕获 在屏幕捕获区域内。 我想要一个定义的部分,其中包含要捕获的图像和内容。我们怎样才能做到这一点?帮助! 访问:https://stackblitz.com/edit/ngx-capture-
从 Perl 脚本调用外部程序时,Capture::Tiny 是否避免了使用 system() 时需要的磁盘 io?使用任何一种时,我都能获得基本相同的性能。一位同事正在使用我的代码并告诉我它正在敲打
作为数值方法研究的一部分,我正在编写一个函数来解决流值问题。这是该程序的“核心”,但它出现了一些奇怪的错误,这很奇怪,因为我在其他程序中使用了相同的代码段而没有出现任何错误。 void solve_
vector vec; //a auto foo = [&vec](){ //do something }; //b auto foo = [&v = vec](){ //do som
我正在使用 PyDev 对我的 Python 应用程序进行开发和单元测试。至于单元测试,除了没有内容被记录到日志框架之外,一切都很好。 PyDev 的“捕获的输出”没有捕获记录器。 我已经将记录的所有
你能帮我解决这个编译器错误吗? template static void ComputeGenericDropCount(function func) { T::ForEach([](T *w
第一次做泛型,我有点困惑。 我有以下内容: public interface GenericDao { /** * Retrieve an object that was previ
我正在尝试提取此代码中 dir_entry.path() 的值并想将其复制到 compFileName 中。问题是我一直收到错误“compFileName cannot be implicitly c
我正在使用在网上找到的 WebCam_Capture 代码通过 C# 访问网络摄像头。在一台只有一个视频源的计算机上,它就像一个魅力! (程序在启动时启动,找到网络摄像头并正常工作)。 虽然在一台有很
下面的代码 void CMainWindow::someMethod(const CLocationsCollection& parentItem) { auto f = [this, par
所以我打开了一个 youtube 页面,我可以在那里观看视频。 但是这个视频被用户下架了。我打开的页面仍然有视频,如果你再次访问(刷新)新页面没有。 由于我在浏览器选项卡 (chrome) 中加载了视
我是一名优秀的程序员,十分优秀!