Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #3035
| From | Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Novice to Generics Trying to Implement a Generic Priority Queue |
| Date | 2011-04-11 21:42 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <invlj0$ibd$1@dont-email.me> (permalink) |
| References | <95a0645f-5c83-4028-8d82-259f83e45159@k9g2000yqi.googlegroups.com> <jbl0q6p0rcaa3olndpqe7r10qtij1joatt@4ax.com> <25ad9459-6d49-4f16-8eb9-d40bec3fc2e2@p13g2000yqh.googlegroups.com> <generic-array-20110411195050@ram.dialup.fu-berlin.de> <138ac7bc-69e3-458b-a49f-881ac1a7bfa5@k11g2000yqc.googlegroups.com> |
On 11/04/2011 21:10, KevinSimonson allegedly wrote:
> On Apr 11, 11:52 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
>> KevinSimonson<kvnsm...@hotmail.com> writes:
>>> Exception in thread "main" java.lang.ClassCastException:
>>> [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
>>
>> ( Da[] )new java.lang.Comparable[ size ]
>
> Stefan, thanks! That solved the problem and my program works just
> fine now.
This might be somewhat OK in this case, but it's hardly advisable.
A Comparable[] /is not a/ Da[].
You'd normally pass the Class object around in such cases:
public PriorityQueue( Class<Da> component, int size )
throws BadSizeException
{
if (0<= size)
{ queue = (Da[]) Array.newInstance( component, size );
nmbrEntries = 0;
}
else {
throw new BadSizeException();
}
}
Since the construction becomes a bit awkward, you can add a factory method:
public static <T extends Comparable> PriorityQueue<T> newQueue( Class<T>
type, int size )
throws BadSizeException
{
return new PriorityQueue<T>( type, size );
}
(Not compiled.)
>
> Kevin Simonson
>
>
> ##############################################################################
>
> Script started on Mon Apr 11 13:03:16 2011
> sh-4.1$ java IntPq 10 314 159 265 358 979 323 846 264 338 327 l
> Added entry 314.
> Added entry 159.
> Added entry 265.
> Added entry 358.
> Added entry 979.
> Added entry 323.
> Added entry 846.
> Added entry 264.
> Added entry 338.
> Added entry 327.
> 0: [979]
> 1: [358]
> 3: [338]
> 7: [159]
> 8: [264]
> 4: [327]
> 9: [314]
> 2: [846]
> 5: [265]
> 6: [323]
> 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)
> throws BadSizeException
> {
> if (0<= size)
> { queue = (Da[]) new java.lang.Comparable[ size];
> nmbrEntries = 0;
> }
> else
> { throw new BadSizeException();
> }
> }
>
> public boolean hasEntries ()
> {
> return 0< nmbrEntries;
> }
>
> public boolean hasRoom ()
> {
> return nmbrEntries< queue.length;
> }
>
> public void addEntry ( Da entry)
> throws OverflowException
> {
> if (queue.length == nmbrEntries)
> { throw new OverflowException();
> }
> Da parent;
> int index;
> int searcher;
> for ( searcher = nmbrEntries++
> ; 0< searcher
> && (parent = queue[ index = searcher - 1>>
> 1]).compareTo( entry)<= 0
> ; searcher = index)
> { queue[ searcher] = parent;
> }
> queue[ searcher] = entry;
> }
>
> public Da extract ()
> throws UnderflowException
> {
> if (nmbrEntries == 0)
> { throw new UnderflowException();
> }
> Da extractee = queue[ 0];
> Da rplcmnt = queue[--nmbrEntries];
> int searcher = 0;
> int lastborn;
> int lrgrChld;
> for (;;)
> { lastborn = searcher + 1<< 1;
> if (nmbrEntries< lastborn)
> { break;
> }
> lrgrChld
> = lastborn< nmbrEntries
> && queue[ lastborn - 1].compareTo( queue[ lastborn])<= 0
> ? lastborn
> : lastborn - 1;
> if (queue[ lrgrChld].compareTo( rplcmnt)<= 0)
> { break;
> }
> queue[ searcher] = queue[ lrgrChld];
> searcher = lrgrChld;
> }
> queue[ searcher] = rplcmnt;
> return extractee;
> }
>
> private void listTree ( int subroot
> , int indnttn)
> {
> if (subroot< nmbrEntries)
> { int spc;
> for (spc = indnttn; 0< spc; spc--)
> { System.out.print( ' ');
> }
> System.out.println( subroot + ": [" + queue[ subroot] + ']');
> subroot = (subroot<< 1) + 1;
> indnttn += 2;
> listTree( subroot , indnttn);
> listTree( subroot + 1, indnttn);
> }
> }
>
> public void list ()
> {
> listTree( 0, 0);
> }
> }
> sh-4.1$ exit
> exit
> Script done on Mon Apr 11 13:04:29 2011
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