Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #3002
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | Lew <noone@lewscanon.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Novice to Generics Trying to Implement a Generic Priority Queue |
| Date | Fri, 08 Apr 2011 20:39:25 -0400 |
| Organization | albasani.net |
| Lines | 83 |
| Message-ID | <ino9ra$52d$1@news.albasani.net> (permalink) |
| References | <95a0645f-5c83-4028-8d82-259f83e45159@k9g2000yqi.googlegroups.com> <inll2n$lsc$1@dont-email.me> <c5f3fb99-c4d9-4eec-8ed4-cadf73b83bbe@x18g2000yqe.googlegroups.com> <ino3nm$3s9$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.albasani.net 5Q1U4+/hPtTvXGmWsGJnQGOxtfDD590NJ6o58hKcDzJmjPRXDBu6jbrpELmwl35a3unP0mXv6YEJi1znCFtuDA== |
| NNTP-Posting-Date | Sat, 9 Apr 2011 00:39:07 +0000 (UTC) |
| Injection-Info | news.albasani.net; logging-data="DeK6mKvG5dxxt9bBFcyr/TbH5IPjwKIYhoyvjYO1ypRY+f8GvKvc+ZtvBtyb277YyOeOoxizGQXolZUXpLuYTQ3U8XYhvMwCKMfKDxJ6nyE/a9iiwXCkI4czXT4qjWn2"; mail-complaints-to="abuse@albasani.net" |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 |
| In-Reply-To | <ino3nm$3s9$1@dont-email.me> |
| Cancel-Lock | sha1:dvToxf5rxWLd5p7wAQiXTW+bU9E= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3002 |
Show key headers only | View raw
markspace wrote:
> KevinSimonson wrote:
>> public PriorityQueue ( int size)
>> {
>> if (0<= size)
>> { @SupressWarnings( "unchecked")
>> queue = (Da[]) new Object[ size];
>
>> PriorityQueue.java:14:<identifier> expected
>> queue = (Da[]) new Object[ size];
Kevin, you really do a fine job of posting a question with a good example,
well presented.
> Interesting. I thought I could put that annotaion on an assignment. I guess
> not. Oh well.
Annotations are allowed only at declarations.
> public PriorityQueue(int size) throws BadSizeException {
> if (0 <= size) {
> @SuppressWarnings("unchecked")
> Da[] temp = (Da[]) new Object[ size ];
> queue = temp;
> nmbrEntries = 0;
> } else {...
>> private static boolean inOrder ( Da left
>> , Da right)
>> {
>> return left.compareTo< ? super Da>( right)<= 0;
>> }
>
>> PriorityQueue.java:25: illegal start of expression
>> return left.compareTo< ? super Da>( right)<= 0;
> This bit goes on the declaration, not the invocation ;-)
>
> public class PriorityQueue<Da extends Comparable<? super Da>> {...
In the actual use, you don't need any angle-brackety stuff:
private static boolean inOrder( Da left, Da right)
{
return left.compareTo( right ) <= 0;
}
You do, however, have to guard against a possible 'NullPointerException'. The
method is 'private', so it's up to its callers not to screw that up. You
enforce that with an assertion:
private static boolean inOrder( Da left, Da right)
{
assert left != null;
return left.compareTo( right ) <= 0;
}
If the method were 'public' you'd need to add an explicit guard against the
'NullPointerException':
public static boolean inOrder( Da left, Da right)
{
if ( left == null )
{
return true;
}
assert left != null;
return left.compareTo( right ) <= 0;
}
The assertion is rather trivial here, so you could simply:
public static boolean inOrder( Da left, Da right)
{
return left == null || left.compareTo( right ) <= 0;
}
Don't forget your Javadocs!
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-07 16:03 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue markspace <-@.> - 2011-04-07 17:32 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-08 14:55 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue markspace <-@.> - 2011-04-08 15:54 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue Lew <noone@lewscanon.com> - 2011-04-08 20:39 -0400
Re: Novice to Generics Trying to Implement a Generic Priority Queue Tom Anderson <twic@urchin.earth.li> - 2011-04-09 10:36 +0100
Re: Novice to Generics Trying to Implement a Generic Priority Queue Roedy Green <see_website@mindprod.com.invalid> - 2011-04-09 05:50 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-11 08:08 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue markspace <-@.> - 2011-04-11 10:29 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue KevinSimonson <kvnsmnsn@hotmail.com> - 2011-04-11 12:10 -0700
Re: Novice to Generics Trying to Implement a Generic Priority Queue Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-11 21:42 +0200
Re: Novice to Generics Trying to Implement a Generic Priority Queue Tom Anderson <twic@urchin.earth.li> - 2011-04-11 22:41 +0100
Re: Novice to Generics Trying to Implement a Generic Priority Queue Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-04-12 01:07 +0200
csiph-web