In Java, why can't iterator return a concrete data type. For example:
//access via Iterator
Iterator iterator = queueA.iterator();
while(iterator.hasNext(){
String element = (String) iterator.next();
}
I have to cast it to String
explicitly.
Why cannot an iterator return String
automatically?
Iterator is a generic class, just like Queue. You should declare your iterator as such: