The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Klemens Katterbauer<br />Student of Mathematics,<br />University of Vienna
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
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 .
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 ]
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
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
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Originally posted by Nabila Mohammad:
What about ArrayList.
It uses order by index. So why was it throwing an exception.
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]
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |