Answers to Self-Test Questions
1.
aList.add("Hello");
2.
aList.set(5, "Go");
3. No. The index for
set
must be greater than or equal to 0 and less than the size of the list. Thus, you can replace any existing element, but you cannot place a new element at any higher index. That is, you cannot add an element beyond the last-used index. This situation is unlike that of an array. If an array is partially filled to index 10, you can add an element at index 20, as long as the array is that large.4. No. The index for
add
must be greater than or equal to 0 and less than or equal to the size of the list. Thus, you can insert an element at any index that is currently holding an element, and you can insert an element immediately after the last element, ...
Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.