java - Syntactical issue using generics -


this homework problem:

i have generic class defined follows:

public class priorityqueue<t extends comparable<t>> {     arraylist<t> queue;   public priorityqueue(){     queue = new arraylist<>(); }  void add(t t){     queue.add(t);     collections.sort(queue); }  <t extends comparable<t>> t remove(){     t t = queue.get(0);     queue.remove(t);     return t; } } 

but netbeans showing following error (red-underline) on line t t = queue.get(0):

incompatible types required: t#2 found:    t#1 t#1,t#2 type-variables: t#1 extends comparable<t#1> declared in class priorityqueue t#2 extends comparable<t#2> declared in method <t#2>remove() 

it seems though somehow it's not understanding t type i'm referring in method declaration same 1 referred in type parameter of class. i'm guessing sort of syntax issue.

i wonder if i'm overcomplicating things - seem more logical me if declared method t remove() {. compiles correctly, when try test using driver class follows:

    priorityqueue pq = new priorityqueue<integer>();      int = 10;     int b = 12;     int c = 5;      int d = 9;      pq.add(a);     pq.add(b);     pq.add(c);     pq.add(d);      integer = pq.remove(); 

i error:

incompatible types:  required: integer  found:    comparable 

on line integer = pq.remove();

as obvious, i'm learning how use generics. please me understand i'm going wrong here.

change

priorityqueue pq = new priorityqueue<integer>(); 

to

priorityqueue<integer> pq = new priorityqueue<integer>(); 

remove method should be

t remove() { ... 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -