Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# code segment question #2

Daniel Lucas
Ranch Hand
Posts: 48
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.set(2, "s");
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.

Purushoth Thambu
Ranch Hand
Posts: 425
If you believe it's from r->l then check the output for this

ArrayList list = new ArrayList(10);
list.set(2, "s");

Daniel Lucas
Ranch Hand
Posts: 48
Originally posted by Purushothaman Thambu:
If you believe it's from r->l then check the output for this

ArrayList list = new ArrayList(10);
list.set(2, "s");
ok. I'm not sure what you mean by putting a 10 in the parentheses after the ArrayList, however this is what I'd do:

[P, Q, R, u, z]

setting s at index position 2:

[P, Q, R, s, z]

Adding T to index position 2(and final product):

[P, Q, R, s, T, z]

Henry Wong
author
Marshal
Posts: 21766
85
Originally posted by Daniel Lucas:
ok. I'm not sure what you mean by putting a 10 in the parentheses after the ArrayList, however this is what I'd do:

[P, Q, R, u, z]

setting s at index position 2:

[P, Q, R, s, z]

Adding T to index position 2(and final product):

[P, Q, R, s, T, z]

I understand that when taking a test, and you don't know the answer, you might have to guess... But in this case, this is a mock test. Whether you got it correct, or not, if you don't completely understand it, look it up -- don't continue guessing.

In this case, I suggest you go to the Java Doc for the ArrayList class and try to figure out exactly what the example in the question is doing.

Henry

Daniel Lucas
Ranch Hand
Posts: 48
I do need to learn to use that part of the Java website more. I'll look more into it tomorrow morning in Java class. I'm about to start my Calculus homework now.

Daniel Lucas
Ranch Hand
Posts: 48
It looks like what this example:

... is doing is it's a constructor, specifically the ArrayList(int initialCapacity). So the example above will be treated the same as the AP test question, except it has a capacity of 10 objects. The total number of objects in the list will end up being 6, so the capacity of the list(10) will have no effect in this case.

I was looking back at the programs I've completed this year and found an array, but it's done entirely different. Here is the code:

In the above program it specified the ARRAY_LENGTH = 10, which is what I thought was happening in the example Purushothaman gave.