泛型ArrayList中存储实现了Comparable接口的泛型对象时的边界不匹配错误。

huangapple go评论73阅读模式
英文:

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&lt;V&gt; implements BucketListADT&lt;V&gt; {

	//The Error underline under &#39;Entry&#39; should clear once you make the
	//Entry object properly comparable. See Entry.java
	private ArrayList&lt;MySortedArray&lt;Entry&lt;V&gt;&gt;&gt; list;
	private int min;
	private int max;

Here is my Entry class that needs to implement Comparable:

public class Entry &lt;V extends Comparable&lt;V&gt;&gt; {
	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()+&quot;=&quot;+this.getKey();
	}
	
	public int compareTo(int key2){
		if(key &gt; key2) return 1; 
    if(key &lt; key2) return -1; 
    else           return 0;
	}
}

Should it be something besides &lt;V extends Comparable&lt;V&gt;&gt; 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 &lt;V extends Comparable&lt;V&gt;&gt; implements Comparable&lt;Entry&lt;V&gt;&gt; { ...

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 &lt;V extends Comparable&gt; to &lt;V&gt;

答案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 &lt;V extends Comparable&lt;V&gt;&gt; implements Comparable&lt;Entry&lt;V&gt;&gt; {
	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()+&quot;=&quot;+this.getKey();
	}
	

	public int compareTo(Entry entry){
		if(key &gt; entry.key) return 1; 
    if(key &lt; entry.key) return -1; 
    else           return 0;
	}

}

huangapple
  • 本文由 发表于 2020年10月13日 09:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64327092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定