SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Implementing A Collection Class
Exercise:
Programming Projects
Question:3 | ISBN:9780136091813 | Edition: 2

Question

The actual ArrayList class in the java.util package has a method called subList that returns a view of a subportion of a list through a given range of indexes. It can be useful to think of part of a list as if it were its own list, complete with its own set of indexes and values. The sublist is “backed” by the original list, meaning that it is not a copy; if any change is made to the sublist, the original list is also affected.

In order to implement this method, you will need to write an inner class inside ArrayList<E> that extends ArrayList and implements the behavior of the sublist. Override the methods for getting and setting values at particular indexes, as well as the size method, so that they reflect the sublist’s index range and size. Also, modify the outer ArrayList<E> class so that it always refers to its own elements through the use of these methods. The outer class should be given the following new method that returns an object of your new inner sublist class:

public ArrayList<E> subList(int fromIndex, int toIndex)

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.