Kyle Hamilton
asked on
JAVA generics
I was wondering if someone could explain this syntax.
I'm working on an implementation of a HashTable with chaining. My rehash function looks like this:
This was the only way I was able to get it to compile.
What I don't understand is why in the inner loop I have to say entry is an Object, and then cast it to Entry<K,V> e inside the loop. It seems weird to do it this way. I *think* I have tried every other permutation, such as: for(Entry entry : bucket)... etc... to no avail.
Thanks,
Koza
I'm working on an implementation of a HashTable with chaining. My rehash function looks like this:
private void rehash() {
DoublyLinkedList<Entry<K, V>>[] oldTable = table;
table = new DoublyLinkedList[2 * oldTable.length];
numKeys = 0;
for( DoublyLinkedList<Entry<K, V>> bucket : oldTable ){
if( bucket != null ){
for(Object entry : bucket){
Entry<K,V> e = (Entry<K,V>) entry;
put((K) e.getKey(), (V) e.getValue() );
}
}
}
}
This was the only way I was able to get it to compile.
What I don't understand is why in the inner loop I have to say entry is an Object, and then cast it to Entry<K,V> e inside the loop. It seems weird to do it this way. I *think* I have tried every other permutation, such as: for(Entry entry : bucket)... etc... to no avail.
Thanks,
Koza
ASKER
hi Doug,
the thing is, i'm trying to use my own *schmucky* data structures :), hense, DoublyLinkedList.
do you think the problem could be in my DoublyLinkedList?
thanks,
Koza
the thing is, i'm trying to use my own *schmucky* data structures :), hense, DoublyLinkedList.
do you think the problem could be in my DoublyLinkedList?
thanks,
Koza
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes. I implemented iterable (see snippet). That is the only piece from java.util I'm using.
I would like to be able to do this without using java.util.Map; Is it possible?
It seems my DoubleLinkedList works for everything else I've done with it, so maybe I've screwed up my iterator implementation somehow.
@Override
public Iterator<E> iterator(){
return new Iter();
};
private class Iter extends DoublyLinkedList<E> implements Iterator<E> {
// more stuff
I would like to be able to do this without using java.util.Map; Is it possible?
It seems my DoubleLinkedList works for everything else I've done with it, so maybe I've screwed up my iterator implementation somehow.
ASKER
ok, so I ran your code as is, and it compiles, of course. Then I used my implementation of DoublyLinkedList, and I get the error I'm getting in my own code. This would suggest there is something wrong with my linked list implementation. back to the drawing board.
thanks again.
Koza.
thanks again.
Koza.
If you want to post the code for DoublyLinkedList (perhaps start another question?) I'll be happy to have a look and see if we can sort out where the problem is.
I'd focus on the type returned by the iterator() method to start with...
Doug
I'd focus on the type returned by the iterator() method to start with...
Doug
ASKER
Thanks Doug,
I'll post another question. I had several implementations of Iterator. all of them worked, and all of them with the same side effect. I thought the fault lay with my iterator, but I don't think so anymore. It's gotta be something else.
I'll post another question. I had several implementations of Iterator. all of them worked, and all of them with the same side effect. I thought the fault lay with my iterator, but I don't think so anymore. It's gotta be something else.
ASKER
OMG, I fixed it. I'm such a dope. I forgot to add the Type to Iterable, like this:
public class DoublyLinkedList<E> implements DoublyLinkedListInt<E>, Iterable<E>{}
I'm gonna leave that other question open to see what else pops up :)
You're the best, Thank you!
public class DoublyLinkedList<E> implements DoublyLinkedListInt<E>, Iterable<E>{}
I'm gonna leave that other question open to see what else pops up :)
You're the best, Thank you!
Then you can use:
for(Map.Entry<K,V> entry : bucket) { }
This compiles just fine for me:
Open in new window
Doug