You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wonder what are the advantages of using different types of generic type indications? You can implement class in two ways:
You can use generic type parameters in the inner classes and in the corresponding instance variables.
or you might not want it.
To reproduce
First version
This version uses generic type parameter in the implementation of the class Deque:
publicclassDeque<Item> implementsIterable<Item> {
privateNode<Item> first;
privateNode<Item> last;
privateintcount;
privatestaticclassNode<Item> {
privateNode<Item> next;
privateNode<Item> prev;
privateItemitem;
}
...
publicvoidaddFirst(Itemitem) {
...
Node<Item> newNode = newNode<>();
...
}
...
publicIterator<Item> iterator() {
// I don't use parameter in this versionreturnnewDoubleLinkedListIterator();
}
// somehow you can leave DoubleLinkedListIterator without <Item> parameterprivateclassDoubleLinkedListIteratorimplementsIterator<Item> {
privateNode<Item> current = first;
// we don't need constructor this time
...
@OverridepublicItemnext() {
if (!hasNext()) thrownewNoSuchElementException();
ItemcurrItem = current.item;
current = current.next;
returncurrItem;
}
}
...
}
Notice that we use static inner class Node. In that way we can save 8 bytes getting rid of inner class extra overhead. Also, somehow you can leave DoubleLinkedListIterator without parameter.
Second Version
This version doesn't use generic type parameters in the implementation of the class Deque:
Different indications of generics types in Java
Description
I wonder what are the advantages of using different types of generic type indications? You can implement class in two ways:
To reproduce
First version
This version uses generic type parameter in the implementation of the class Deque:
Notice that we use static inner class Node. In that way we can save 8 bytes getting rid of inner class extra overhead. Also, somehow you can leave DoubleLinkedListIterator without parameter.
Second Version
This version doesn't use generic type parameters in the implementation of the class Deque:
Possible answer
1.Here?
2.Maybe here?
The text was updated successfully, but these errors were encountered: