Alright this is question 2 from the collegeboard's practice AP Comp Sci A
test questions on AP Central:
Consider the following code segment.
ArrayList list = new ArrayList ();
list.add("P");
list.add("Q");
list.add("R");
list.set(2, "s");
list.add(2, "T");
list.add("u");
System.out.println(list);
What is printed as a result of executing the code segment?
(A) [P, Q, R, s, T]
(B) [P, Q, s, T, u]
(C) [P, Q, T, s, u]
(D) [P, T, Q, s, u]
(E) [P, T, s, R, u]
My analysis:
OK so we've got a new ArrayList named list. Next it's adding P, Q, and R to the ArrayList. Now here is where I think I'm going to have trouble. Due to my limited knowledge of ArrayLists, I'm unsure of what this expression means:
list.set(2, "s");
I'm not sure what set means or what it means having 2 arguments in the parentheses.
It goes on and does an add similar to the set, except with a T instead of an s. Finally, it adds a u and prints out the value of list.
So can I answer this question without knowing everything about ArrayLists? hmm. Well this is usually futile with collegeboard questions, but I'm going to look at the answer choices and see if it sparks some sort of mental reminder.
A - adds a P, Q, R, then has an s then T at the end. How would that correspond to placing a 2 in front of s and T? Also, there is no u at the end. Shouldn't there be a u at the end? I'm going to eliminate choice A.
B - adds a P, Q, then s and T with a u at the end. Where'd the R go? Since this has no R, I'm going to eliminate choice B.
C - adds P, Q, then T and s, and finally u. Again there is no R. I'm going to eliminate choice C because there is no R and I think there should be one.
D - adds P, then T, then adds Q, then s, then adds u. This has no R, but it looks kind of like it's the right answer... but I have no factual basis to support this. Hmmm. I'll go on to E for now.
E - adds P, then T, then s, then adds R and u.
I'm thinking, well there has to be some meaning to set over add. Like set takes precendence over add. And maybe that 2 means the argument after it (s or T) is the 2nd number in the ArrayList. Going by that thinking I'd get:
[P, s, T, Q, R, u]
That's clearly not one of the choices. I'm getting a bit impatient now, lol... *starts eyeballing*
OK, I'm clearly stuck, so I did a bit of research on list.add and list.set.
The definitions:
list.set(index,obj) -- stores an object obj at position number index in the list, replacing the object that was there previously. This does not change the number of elements in the list or move any of the other elements.
list.add(index,obj) -- inserts an object obj into the list at position number index. The number of items in the list increases by one, and items that come after position index move up one position to make room for the new item. The value of index can be in the range 0 to list.size(), inclusive.
OK I was sort of on the right track here. So set replaces the item at number index in the list, but does not change the number of elements in the list or move any of the other elements, which is why there are still only 5 letters listed in the ArrayList instead of 6. Add, on the other hand, inserts an extra object into the list at the position number index(2). This does increase the number of items in the list by one in order to make room for the new object.
So my questions now:
1. Does set replace the add since they are in the same index position? I notice set comes before add.
2. Where does index position 2 actually correspond to on the ArrayList? Is that the second object, in the answer choices either Q or T?
Well I'm going to assume index position 2 is the second object in the list.
SO we'd normally have:
[P, Q, R, u]
after setting s into the list at index position 2:
[P, s, R, u]
after adding T into the list at index position 2:
[P, T, s, R, u]
That's an answer choice! *checks back at answer choices* The answer is C, not E. It looks like the index position and index 2 is the second to last number of the ArrayList. So let me go through the process again:
[P, Q, R, u]
Setting s at index position 2:
[P, Q, s, u]
Adding T to index position 2:
[P, Q, s, T, u]
So apparently if I add T there it bumps the object already in place at index position 2 behind it instead of in front of it.
I can conclude that an ArrayList's position index is read from right to left?
Well, I guess so unless one of you corrects me! I think I'm right though.