Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!postnews.google.com!x18g2000yqe.googlegroups.com!not-for-mail From: KevinSimonson Newsgroups: comp.lang.java.programmer Subject: Re: Novice to Generics Trying to Implement a Generic Priority Queue Date: Fri, 8 Apr 2011 14:55:35 -0700 (PDT) Organization: http://groups.google.com Lines: 198 Message-ID: References: <95a0645f-5c83-4028-8d82-259f83e45159@k9g2000yqi.googlegroups.com> NNTP-Posting-Host: 216.49.181.254 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1302299736 25210 127.0.0.1 (8 Apr 2011 21:55:36 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 8 Apr 2011 21:55:36 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: x18g2000yqe.googlegroups.com; posting-host=216.49.181.254; posting-account=gsXmKQkAAADuyNfZpkeBrCCcqQvlDvn9 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; InfoPath.2; .NET4.0C; .NET4.0E),gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2998 On Apr 7, 6:32=A0pm, markspace <-@.> wrote: > > This is a bit of a flaw in Java's generics. =A0The generic types aren't > reifiable, so there's actually no type at run time for the JVM to use to > create the array. =A0Sometimes you can go around this limitation. > Sometimes you can't. =A0Here's the case for when you can't. > > @SupressWarnings("unchecked") > =A0 =A0queue =3D (Da[]) new Object[size]; > > Don't use SupressWarnings unless you have to, but in this case you have t= o. > > > > > The rest of the compilation errors seem to be referring to my use of > > method > > "compareTo< =A0Da>()". =A0Can anyone tell me what I'm doing wrong here? > > Comparable is a bit of a weird one. =A0With out going into details, you > need "Comparable" on the class declaration to get the type > right. > > I didn't read through the rest of your code, but hopefully this gets you > pointed in the right direction. markspace, thanks for the pointers. I made the changes you suggested, or at least I attempted to; maybe you can look at my code and see if I messed anything up. Anyhow, I try to compile it and get the error messages I'm attaching. Can you or anyone else tell me what I'm doing wrong? Kevin Simonson =20 ###########################################################################= ### Script started on Fri Apr 8 15:47:37 2011 sh-4.1$ cat PriorityQueue.java public class PriorityQueue< Da extends Comparable> { public static class BadSizeException extends Exception {} public static class UnderflowException extends Exception {} public static class OverflowException extends Exception {} Da[] queue; int nmbrEntries; public PriorityQueue ( int size) { if (0 <=3D size) { @SupressWarnings( "unchecked") queue =3D (Da[]) new Object[ size]; nmbrEntries =3D 0; } else { throw new BadSizeException(); } } private static boolean inOrder ( Da left , Da right) { return left.compareTo< ? super Da>( right) <=3D 0; } public boolean hasEntries () { return 0 < nmbrEntries; } public boolean hasRoom () { return nmbrEntries < queue.length; } public void addEntry ( Da entry) { if (queue.length =3D=3D nmbrEntries) { throw new OverflowException(); } Da parent; int index; int searcher; for ( searcher =3D nmbrEntries++ ; 0 < searcher && inOrder( parent =3D queue[ index =3D searcher - 1 >> 1], entry) ; searcher =3D index) { queue[ searcher] =3D parent; } queue[ searcher] =3D entry; } public Da extract () { if (nmbrEntries =3D=3D 0) { throw new UnderflowException(); } Da extractee =3D queue[ 0]; Da rplcmnt =3D queue[--nmbrEntries]; int searcher =3D 0; int lastborn; int lrgrChld; for (;;) { lastborn =3D searcher + 1 << 1; if (nmbrEntries < lastborn) { break; } lrgrChld =3D lastborn < nmbrEntries && inOrder( queue[ lastborn - 1], queue[ lastborn]) ? lastborn : lastborn - 1; if (inOrder( queue[ lrgrChld], rplcmnt)) { break; } queue[ searcher] =3D queue[ lrgrChld]; searcher =3D lrgrChld; } queue[ searcher] =3D rplcmnt; return extractee; } public void list () { int index; for (index =3D 0; index < nmbrEntries; index++) { System.out.println( index + ": [" + queue[ index] + ']'); } } } sh-4.1$ javac PriorityQueue.java PriorityQueue.java:14: expected queue =3D (Da[]) new Object[ size]; ^ PriorityQueue.java:25: illegal start of expression return left.compareTo< ? super Da>( right) <=3D 0; ^ PriorityQueue.java:25: '.' expected return left.compareTo< ? super Da>( right) <=3D 0; ^ PriorityQueue.java:25: : expected return left.compareTo< ? super Da>( right) <=3D 0; ^ PriorityQueue.java:25: ';' expected return left.compareTo< ? super Da>( right) <=3D 0; ^ PriorityQueue.java:28: illegal start of expression public boolean hasEntries () ^ PriorityQueue.java:28: ';' expected public boolean hasEntries () ^ PriorityQueue.java:28: ';' expected public boolean hasEntries () ^ PriorityQueue.java:33: illegal start of expression public boolean hasRoom () ^ PriorityQueue.java:33: ';' expected public boolean hasRoom () ^ PriorityQueue.java:38: illegal start of expression public void addEntry ( Da entry) ^ PriorityQueue.java:38: illegal start of expression public void addEntry ( Da entry) ^ PriorityQueue.java:38: ';' expected public void addEntry ( Da entry) ^ PriorityQueue.java:38: ';' expected public void addEntry ( Da entry) ^ PriorityQueue.java:55: illegal start of expression public Da extract () ^ PriorityQueue.java:55: ';' expected public Da extract () ^ PriorityQueue.java:85: illegal start of expression public void list () ^ PriorityQueue.java:85: illegal start of expression public void list () ^ PriorityQueue.java:85: ';' expected public void list () ^ PriorityQueue.java:92: reached end of file while parsing } ^ 20 errors sh-4.1$ exit exit Script done on Fri Apr 8 15:48:01 2011