简体   繁体   English

如何子类化ArrayList,并要求 <E> 必须扩展可比

[英]How do I subclass ArrayList, and require that <E> must extend Comparable

I think what I'm trying to do is clear, but I'm no Generics expert. 我认为我想做的事情很明确,但我不是泛型专家。

import java.util.ArrayList;
public class MinHeap<E extends Comparable> extends ArrayList<E>  {
    /* A simple wrapper around a List to make it a binary min-heap. */
    public MinHeap() {
        super();
    }

    @Override
    public boolean add(E e) {
        super.add(e);
        int i = this.size() - 1;
        int parent;

        while (i > 0) {
            parent = this.getParentIndex(i);

            if (this.get(i).compareTo(this.get(parent)) < 0) {
                this.swap(i, parent);
            } else {
                break;
            }
        }
        return true;
    }

    public int getParentIndex(int i) {
        if (i % 2 == 1) {
            return (i - 1) / 2;
        } else {
            return (i - 2) / 2;
        }
    }

    private void swap(int i, int j) {
        E temp = this.get(i);
        this.set(i, this.get(j));
        this.set(j, temp);
    }
}

I get a warning at compile-time: 我在编译时收到警告:

MinHeap.java:21: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable
            if (this.get(i).compareTo(this.get(parent)) < 0) {
                                 ^
  where T is a type-variable:
    T extends Object declared in interface Comparable
1 warning

which I don't understand. 我不明白。 What am I missing? 我想念什么?

I initially thought that it had to do with needing to check the that this.get(i) and this.get(parent) were instances of Comparable ... so I added a check: 我最初认为这与需要检查this.get(i)和this.get(parent)是Comparable的实例有关...因此我添加了一个检查:

if (!(this.get(i) instanceof Comparable) ||
    !(this.get(parent) instanceof Comparable)) {
    return false;
}

But that gives the same warning. 但这给出了同样的警告。

public class MinHeap<E extends Comparable> extends ArrayList<E> 

should be 应该

public class MinHeap<E extends Comparable<E>> extends ArrayList<E> 

since Comparable is a generic interface itself. 因为Comparable本身就是通用接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用扩展类 <E extends Comparable<E> &gt; - How to extend Generic abstract Class with <E extends Comparable<E>> 我应该实现或扩展可比构造函数以及如何? - Should i implement or extend comparable to the constructor and how? 如何添加到 ArrayList<student> 来自文件的学生,但如果它来自另一个子类,我该如何添加它? 例如:Unit_Common?</student> - How to add to an ArrayList <Student> students from a file but how do I add it if it is from another subclass? E.g: Unit_Common? 如何转换ArrayList <LinkedHashMap<String, Comparable> &gt;可比? - How to cast ArrayList<LinkedHashMap<String, Comparable>> to Comparable? 如何在ArrayList中使用Comparator和Comparable? - How to use Comparator and Comparable in ArrayList? 当我在Java中扩展ArrayList时,如何添加地图和过滤器? - How do I add map and filter when I extend ArrayList in Java? 扩展ArrayList <E> 当E是ArrayList本身的嵌套类时,这是错误的吗? - Extend ArrayList<E> when E is a nested class of the ArrayList itself…is this wrong? 如何让我的泛型类要求其参数扩展/实现多个类? - How do I get my generic class to require its parameters to extend/implement multiple classes? Java Serializable:必须在ArrayList中可序列化 <E> ? - Java Serializable: Must E be serializable in ArrayList<E>? 如何添加两个不同类的ArrayList,将一个抽象类扩展为一个ArrayList? - How do I add two ArrayLists of different classes that extend an abstract class into one ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM