/** * Replaces the last element returned by either next or * previous. Cannot be
ID: 3913191 • Letter: #
Question
/** * Replaces the last element returned by either next or * previous. Cannot be called immediately after add * or remove. */
void set(E obj); This is a method declared in listIterator which implements Iterator.
In what classes can I implement this method?
These are the classes: (AbstractCollection(implements Collection), ArrayCollection(extends AbstractCollection), LinkedCollection(extends AbstractCollection), AbstractList(Implements List), ArrayList(extends AbstractList).
And in what classes should I implement to get maximum efficiency?
Explanation / Answer
set() must be implemented in case of ArrayCollection, LinkedList, AbstractList and ArrayList. This is necessary.
However, you can also implement it in AbstractCollection as your own function.
Now talking about performance, the best overall performance is of ArrayList. But if the additions and deletions are more, then the LinkedList implementation becomes better.
In case of ArrayList -
Get - O(1)
Add - O(1)
Remove - O(n)
In case of LinkedList
Get - O(n)
Add - O(1)
Remove - O(n)
Thus if insertions and removals are more, linked list is better otherwise if access are more then ArrayList is better.
The interfaces cannot be compared with these as their peformances are not defined unless implemented.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.