英文:
Bound Mismatch with generic arraylist that stores generic objects that implement comparable
问题
我的老师说,一旦我在我的Entry类中实现了Comparable接口,bound mismatch错误就应该消失。
这里产生了错误:
public class BucketList<V> implements BucketListADT<V> {
// 一旦你正确地使Entry对象可比较,'Entry'下面的错误划线应该消失。请查看Entry.java
private ArrayList<MySortedArray<Entry<V>>> list;
private int min;
private int max;
}
这是我需要实现Comparable接口的Entry类:
public class Entry<V extends Comparable<V>> {
private int key;
private V val;
public Entry(int k, V v) {
key = k;
val = v;
}
public int getKey() {
return this.key;
}
public V getValue() {
return this.val;
}
public String toString() {
return this.getValue() + "=" + this.getKey();
}
public int compareTo(int key2) {
if (key > key2) return 1;
if (key < key2) return -1;
else return 0;
}
}
在Entry
类的声明中,除了<V extends Comparable<V>>
,还应该是其他什么内容吗?
英文:
My teacher was saying that the bound mismatch error should disappear once I implement comparable in my Entry class.
This generates the error:
public class BucketList<V> implements BucketListADT<V> {
//The Error underline under 'Entry' should clear once you make the
//Entry object properly comparable. See Entry.java
private ArrayList<MySortedArray<Entry<V>>> list;
private int min;
private int max;
Here is my Entry class that needs to implement Comparable:
public class Entry <V extends Comparable<V>> {
private int key;
private V val;
public Entry(int k, V v) {
key = k;
val = v;
}
public int getKey() {
return this.key;
}
public V getValue() {
return this.val;
}
public String toString() {
return this.getValue()+"="+this.getKey();
}
public int compareTo(int key2){
if(key > key2) return 1;
if(key < key2) return -1;
else return 0;
}
}
Should it be something besides <V extends Comparable<V>>
in the Entry
class declaration?
答案1
得分: 0
以下是翻译好的部分:
"要知道更多关于您所遇到的错误将非常有用。到目前为止,我可以告诉您的是,您的 Entry 类没有实现 Comparable 接口。您的代码表明 V 实现了 Comparable 接口。也许这是您想要的,也可能不是。如果要让其他代码知道 Entry 实现了 Comparable 接口,您应该这样写:
public class Entry <V extends Comparable<V>> implements Comparable<Entry<V>> { ...
这是如果您打算让 V 也实现 Comparable 接口。在这种情况下,您还应该在 BucketList 中进行相同的指定,否则 BucketList 表示它可以具有任何 V,即使它不是 Comparable,但在之后立即将其用作如果它被保证是 Comparable。如果您不打算让 V 必须实现 Comparable 接口,只需将 <V extends Comparable>
更改为 <V>
。"
英文:
It would really be useful to know a bit more about the error you are getting. What I can tell you so far is that your Entry class does no implement Comparable. What you are saying with that code is that V implements Comparable. Maybe you want that, maybe not. To let the rest of the code know that Entry implements Comparable you should write:
public class Entry <V extends Comparable<V>> implements Comparable<Entry<V>> { ...
That is if you intend for V to also be Comparable. In that case you should also specify it like that in BucketList or else BucketList says it may have any V, even if it is not Comparable, but immediately after uses it as if it where guaranteed to be Comparable. If you don't intend for V to necessarily implement Comparable, just change <V extends Comparable>
to <V>
答案2
得分: 0
感谢'Manuel Rene Pauls Toews'的帮助。
这是现在正常工作的Entry类:
public class Entry<V extends Comparable<V>> implements Comparable<Entry<V>> {
private int key;
private V val;
public Entry(int k, V v) {
key = k;
val = v;
}
public int getKey() {
return this.key;
}
public V getValue() {
return this.val;
}
public String toString() {
return this.getValue() + "=" + this.getKey();
}
public int compareTo(Entry<V> entry) {
if (key > entry.key) return 1;
if (key < entry.key) return -1;
else return 0;
}
}
英文:
Thanks to 'Manuel Rene Pauls Toews' for the help.
Here is my Entry class that works properly now:
public class Entry <V extends Comparable<V>> implements Comparable<Entry<V>> {
private int key;
private V val;
public Entry(int k, V v) {
key = k;
val = v;
}
public int getKey() {
return this.key;
}
public V getValue() {
return this.val;
}
public String toString() {
return this.getValue()+"="+this.getKey();
}
public int compareTo(Entry entry){
if(key > entry.key) return 1;
if(key < entry.key) return -1;
else return 0;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论