Free Online Flashcard Deck

Java ArrayList Algorithms and Collections Free Online FlashCards

Study Java ArrayList Algorithms and Collections with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

What are the defining properties of an ArrayList?

Back

ArrayList is an ordered, resizable collection of object references. Its size and elements can change, and duplicate values are allowed.

02
Front

How do add(index, value) and set(index, value) differ?

Back

add(index, value) inserts and increases the size, shifting later elements right. set(index, value) replaces an existing element without changing the size.

03
Front

What indexes are valid for ArrayList access and insertion?

Back

For access or replacement, valid indexes are 0 through size() - 1. An index equal to size() is valid only for insertion with add(index, value).

04
Front

What does remove(1) mean for an ArrayList<Integer>?

Back

In an ArrayList<Integer>, remove(1) removes the element at index 1. To remove the value 1, use remove(Integer.valueOf(1)).

05
Front

Why is i < list.size() usually correct in an index loop?

Back

Use i < list.size(). With i <= list.size(), the loop eventually attempts to access index size(), which is outside the valid range.

06
Front

When should you choose an enhanced for loop?

Back

Use an enhanced for loop when each element is needed but its index is not. Use an index-based loop when the index or controlled modification is required.

07
Front

Why does backward traversal safely remove matching elements?

Back

Traverse from the last index down to zero. Removing an element only shifts elements to its right, which have already been processed in a backward traversal.

08
Front

How can a while loop remove elements without skipping any?

Back

After removing at index i, do not increment i; examine the new element now at i. Increment i only when no removal occurs.

09
Front

Why can’t an ArrayList use int as its type parameter?

Back

Use wrapper types such as Integer, Double, or Boolean because ArrayList stores objects, not primitive values. For example, use ArrayList<Integer>, not ArrayList<int>.

10
Front

What are autoboxing and unboxing?

Back

Autoboxing converts a primitive to its wrapper object, while unboxing converts a wrapper object to its primitive value. Unboxing a null wrapper causes NullPointerException.

11
Front

How should a maximum algorithm initialize its candidate?

Back

Initialize the maximum from the first element, such as numbers.get(0), rather than from 0. Starting at 0 fails when all values are negative.

12
Front

Why does duplicate detection compare j from i + 1?

Back

Start the inner loop at i + 1. This avoids comparing an element with itself and checks each unordered pair only once.