Forums Register Login

Queue

+Pie Number of slices to send: Send
This is a code from K&B .
I thought this prgram would result in an Exception after giving warnings during the compiile time.This is because we are try adding an Integer to a queue typed <String>

However, it gives warnings and runs successfully with out any error or exceptions.

Can some one help me with that..

+Pie Number of slices to send: Send
Hi Mohammad,

Just have a look at the method that's defined.



As you see the method doesn't use generics in its argument list, so the compiler doesn't know what will taken as an argument and therefore lets you add everything you want. It will print nevertheless a warning that you might do an unsafe operation but as I said the compiler won't enforce it.

As generics provide only compile-time protection, the JVM will not issue an error or an exception since all operations are correct.
+Pie Number of slices to send: Send
Hi Klemens..

Thanks for the response.

Check this other code which is also from K&B.
Here it throws and exception and this is what I was expecting.

Here the list is of type Integer and I try to insert a String which results in Exception.
Isnt that a similiar case to the previous code.
I added an Integer to a Queue which was of String type.
How come that got executed and this didn't .
+Pie Number of slices to send: Send
 

Originally posted by Nabila Mohammad:
Hi Klemens..

Thanks for the response.

Check this other code which is also from K&B.
Here it throws and exception and this is what I was expecting.

Here the list is of type Integer and I try to insert a String which results in Exception.
Isnt that a similiar case to the previous code.
I added an Integer to a Queue which was of String type.
How come that got executed and this didn't .



Run this code and try to understand why this one is throwing an exception.



Hint: PriorityQueue uses natural order
[ August 06, 2008: Message edited by: Raphael Rabadan ]
+Pie Number of slices to send: Send
 

Originally posted by Raphael Rabadan:


Run this code and try to understand why this one is throwing an exception.



Hint: PriorityQueue uses natural order

[ August 06, 2008: Message edited by: Raphael Rabadan ]



This is code throwing an Exception becuase you are using PriorityQueue which sorts the queue in Alphabetical order or Priority .

so in this case it can't sort out if there is a String and Integer...
however if it's a Linked list which simply adds to the list..it gets casted as a String?! Right?

What about ArrayList - Does it require Casting? and is that why it throws exception?

So what has this got to with the
+Pie Number of slices to send: Send
 

Originally posted by Nabila Mohammad:


This is code throwing an Exception becuase you are using PriorityQueue which sorts the queue in Alphabetical order or Priority .

so in this case it can't sort out if there is a String and Integer...
however if it's a Linked list which simply adds to the list..it gets casted as a String?! Right?

What about ArrayList - Does it require Casting? and is that why it throws exception?

So what has this got to with the




PriorityQueue sorts the Queue using natural order, it means, using Comparable, or you can sort in other way using a Comparator(if you pass one to the constructor). The only diference of the Queue interface are the methods to get the next in the queue, like .poll(), .peek(), .remove().

LinkedList is ordered by insertion, so, the, when you add something to a list like LinkedList, nothing will be casted, because its not sorted (wont use a comparator or comparable), so wont have any cast, why it would?
When you iterate a Queue without generics its returning Objects, so, no problem too.

That's it.

I hope i could clarify your doubt.


Kind Regards,
Raphael Rabadan.
+Pie Number of slices to send: Send
What about ArrayList.
It uses order by index. So why was it throwing an exception.
+Pie Number of slices to send: Send
 

Originally posted by Nabila Mohammad:
What about ArrayList.
It uses order by index. So why was it throwing an exception.



Since when ArrayList implements Queue? Can you show the example?
+Pie Number of slices to send: Send
 

Originally posted by Nabila Mohammad:
I did not mean ArrayList implementing Queue
Was asking in General...

In this code it throws an exception because we are adding an Integer to a list of type String.

It does not sort the List like a PriorityQueue
It is inserting the element like a linked list except that it is ordered by index and not by insertion.

In that case, why is it throwing an exception?
Simply because it cannot Integer cannot be casted ot String?

I basically cannot understand the link between the different Collections when adding a different type element to it, when it's type is already predefined.

Priorty Queue -> throws exception -> it sorts it in natural order
LinkedList -> doesnot throws exceeption ->it simple inserts the element even if it is not of the type mentioned?
What about ArrayList?




[/QB]

 
+Pie Number of slices to send: Send
This case is completly diferent, you are using a explicit cast.
Remember, even if the List accept Objects, the objets inside of it has your own types.
In this example you showed, it has Integers and Strings, and you are trying to use a explicit cast to a Integer over all the List, when it get on the String object it will throw the ClassCastException, like it:

[ August 07, 2008: Message edited by: Raphael Rabadan ]
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1237 times.
Similar Threads
generics question
Generics
COLLECTION PROBLEM PLEASE EXPLAIN!!
Confusion with Generics
Doubt on PriorityQueue?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:40:49.