java - How do I subclass ArrayList, and require that <E> must extend Comparable -
i think i'm trying clear, i'm no generics expert.
import java.util.arraylist; public class minheap<e extends comparable> extends arraylist<e> { /* simple wrapper around list make binary min-heap. */ public minheap() { super(); } @override public boolean add(e e) { super.add(e); int = 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 warning @ compile-time:
minheap.java:21: warning: [unchecked] unchecked call compareto(t) member of raw type comparable if (this.get(i).compareto(this.get(parent)) < 0) { ^ t type-variable: t extends object declared in interface comparable 1 warning
which don't understand. missing?
i thought had needing check this.get(i) , this.get(parent) instances of comparable ... added check:
if (!(this.get(i) instanceof comparable) || !(this.get(parent) instanceof comparable)) { return false; }
but gives same warning.
public class minheap<e extends comparable> extends arraylist<e>
should
public class minheap<e extends comparable<e>> extends arraylist<e>
since comparable
generic interface itself.
Comments
Post a Comment