Implement correct sub-list behavior from the subList method of the AbstractList
ID: 3531843 • Letter: I
Question
Implement correct sub-list behavior from the subList method of the AbstractList class. One of the cleanest and simplest ways to correctly implement the subList method is to employ the Decorator design pattern. That is, your solution will be a class that decorates a List and a ListIterator object.
Within the AbstractList class, the implementation of subList may be written as:
publicabstract class AbstractList<E>
extends AbstractCollection<E> implements List<E>
{
// ... other methods skipped ...
public List<E> subList(int startIndex, int endIndex)
{
return new SubList<E>(this, startIndex, endIndex);
}
}
The SubList class will keep a reference to the original list and forward modified method calls into the decorated list. SubList will start as something like the following:
public class SubList<E> extends AbstractList<E>
{
// the decorated list
private List<E> list;
private int start;
private int end;
public SubList(List<E> baseList, int fromIndex, int toIndex)
{
}
public int size()
{
return 0;
}
public List<E> subList(int fromIndex, int toIndex)
{
return null;
}
public ListIterator<E> listIterator(int position)
{
return new SubListIterator(position);
}
private class SubListIterator implements ListIterator<E>
{
// the decorated list iterator
private ListIterator<E> iter;
// ... implementations of all iterator methods ...
}
}
Because the SubList class extends AbstractList, there are only a few methods that must be implemented: the constructor, size()and listIterator(). However, overriding subList() will allow sub-lists of sub-lists to be much more efficient by re-decorating the original list rather than decorating an existing decorator. Also, since AbstractList provides implementations that only depend on the list iterator class, the majority of the work of implementing SubList will be done in the iterator. Remember, the goal is to decorate, so the instance field list in SubList and iter in SubListIterator will be the original list and an iterator over the original list, respectively. The actual methods can be carried out by doing some additional processing and then calling the method of the same name within the method being written. For example, the set method of the iterator is by far the easiest and can be written simply as:
publicvoid set(E obj)
{
this.iter.set(obj);
}
The Java documentation for the subList method of the List interface is replicated below:
subList
publicList subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
Parameters:
fromIndex - low endpoint (inclusive) of the subList.
toIndex - high endpoint (exclusive) of the subList.
Returns:
a view of the specified range within this list.
Throws:
IndexOutOfBoundsException - for an illegal endpoint index value (fromIndex < 0 || toIndex > size || fromIndex > toIndex).
Explanation / Answer
FOLLOW THIS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.