Consider the following method hat is intended to modify its parameter nameList by replacing all occurrences of name with newValue
public void replace(ArrayList<String> nameList, String name, String newValue) {
for (int j = 0; j < nameList.size(); j++) {
if (/*expression*/)
namelist.set(j, newValue);
}
}
Which of the following can be used to replace /* expression */ so that replace will work as intended?
(a) nameList.get(j).equals(name)
(b) ...
(c) ...
(d) ...
(e) nameList[j].equals(name)
I chose e, but the correct answer is a. I don't quite understand why a works but not e, and I'm not entirely sure what the difference is between the two...
You can find the question here, on page 9:
http://xhs-java-oop.wikispaces.com/file/view/PracticeProblems.pdf