Page 1269 - AP Computer Science A, 7th edition
P. 1269

40. (E) All three changes must be made! In order to move all the Card elements to the temporary ArrayList, the for loop must be executed size times. If you start j at 1, the loop will be executed size–1 times. The error in Line 7 is subtle. With each iteration of the loop, the size of the cards ArrayList is being reduced by 1, so the range of random indexes is getting smaller and smaller. This won’t happen if you use size, the length of the original cards list. You must use cards.size(), which is the length of the current, shorter list. If you don’t make correction III, the random element will not be removed from cards. It will (incorrectly) remain there while a copy of it will be added to temp. If this error isn’t corrected, execution of the method is likely to cause the temp list to hold more than one copy of a given card!


































































































   1267   1268   1269   1270   1271