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:
Arraylists
Exercise:
Exercises
Question:10 | ISBN:9780136091813 | Edition: 2

Question

Write a method called removeInRange that accepts three parameters, an ArrayList of String s, a beginning String , and an ending String , and removes from the list any String s that fall alphabetically between the start and end String s. For example, if the method is passed a list containing the elements (“to”, “be”, “or”, “not”, “to”, “be”, “that”, “is”, “the”, “question”), “free” as the start String , and “rich” as the end String , the list’s elements should be changed to (“to”, “be”, “to”, “be”, “that”, “the”). The “or”, “not”, “is”, and “question” should be removed because they occur alphabetically between “free” and “rich”. You may assume that the start String alphabetically precedes the ending String.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Implementation of removeInRange method:

	public static void removeInRange(ArrayList strList,
			String start, String end)
	{
		for(int i = 0; i < strList.size(); i++)
		{
			if(strList.get(i).compareTo(start) > 0
					&& strList.get(i).compareTo(end) < 0)
			{
				strList.remove(i);
				i--;
			}

		}
	}
0 0

Discussions

Post the discussion to improve the above solution.