10. Write a method called removeInRange that accepts three parameters, an ArrayL
ID: 3696311 • Letter: 1
Question
10. Write a method called removeInRange that accepts three parameters, an ArrayList of strings, a beginning string, and an ending string, and removes from the list any strings that fall alphabetically between the start and end strings. 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.
Explanation / Answer
ArrayListTest.java
import java.util.ArrayList;
public class ArrayListTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList list= new ArrayList<String>();
list.add("to");
list.add("be");
list.add("or");
list.add("not");
list.add("to");
list.add("be");
list.add("that");
list.add("is");
list.add("the");
list.add("question");
list = removeInRange(list, "free", "rich");
System.out.println(list);
}
public static ArrayList removeInRange(ArrayList<String> list, String beginning , String ending){
char beginChar = beginning.charAt(0);
char endChar = ending.charAt(0);
ArrayList<String> list1 = new ArrayList<String>();
for(int i=0; i< list.size(); i++){
String s = list.get(i);
if(!(s.charAt(0) >= beginChar && s.charAt(0) <= endChar)){
list1.add(s);
}
}
return list1;
}
}
Output:
[to, be, to, be, that, the]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.