Question: Consider the following instance variables and methods. // Two nonempty lists of words private ArrayList list1; private ArrayList list2; /** Checks if a String is
Consider the following instance variables and methods.
// Two nonempty lists of words private ArrayListlist1; private ArrayList list2; /** Checks if a String is contained in an ArrayList * @param aList an ArrayList of String objects * @param target a String to look for * @return true if target is in aList; false otherwise */ private boolean isIn(ArrayList aList, String target) { for(String s : aList) { if(s.equals(target)) { return true; } } return false; } /** Removes duplicate strings from aList * @param aList an ArrayList of String objects * @return an ArrayList containing every unique String from aList but * not containing any String more than once. */ private ArrayList removeDupes(ArrayList aList) { ArrayList output = new ArrayList (); for(String s : aList) { if (! isIn(output, s)) { output.add(s); } } return output; } /** @return an ArrayList containing each of the elements from list1 and list2 * but with no duplicate Strings */ public ArrayList combiList() { ArrayList uList1 = removeDupes(list1); ArrayList uList2 = removeDupes(list2); ArrayList result = new ArrayList (); for (String word : uList1) { result.add(word); } for (String word : uList2) { result.add(word); } return result; }
The combiList method is intended to return an ArrayList which contains every word which appears on list1 or list2 without any duplicate words. For which of the following values of list1 and list2 will the method not work as intended?
list1 = ["blue", "red", "blue"] list2 = ["orange", "orange", "orange"]
list1 = ["blue", "red", "green"] list2 = ["orange", "yellow", "purple"]
list1 = ["blue", "red", "red"] list2 = ["green", "yellow", "purple"]
list1 = ["blue", "red", "yellow"] list2 = ["orange", "yellow", "orange"]
The method will work as intended for all values of list1 and list2.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
