Free Online Flashcard Deck

07 Arrays and Collections Free Online FlashCards

Study 07 Arrays and Collections with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

What is a Java array?

Back

An array is an ordered, fixed-size sequence whose elements all have the same type. Java arrays are objects.

02
Front

What is the valid index range of a Java array?

Back

Java arrays use zero-based indexing: the first index is 0, and the last valid index is `array.length - 1`.

03
Front

What is the difference between declaring and creating an array?

Back

A declaration creates a variable that can refer to an array; `new` creates the array object with a specified length.

04
Front

How do you obtain a Java array's length?

Back

Use the field `array.length`, not the method call `array.length()`, to obtain an array's length.

05
Front

How do you update one element of an array?

Back

Assign a new value through its index, such as `scores[2] = 80;`. The assignment changes that existing element.

06
Front

Why should an array loop use `i < array.length`?

Back

An indexed loop should continue while `i < array.length`. Using `<=` attempts to access the invalid index `array.length`.

07
Front

When is a for-each loop appropriate for an array?

Back

Use a for-each loop when you need each value but not its index. Changing its loop variable does not replace elements in the array.

08
Front

What is a linear search?

Back

A linear search checks elements from beginning to end and works whether or not the array is sorted. Its worst-case running time is O(n).

09
Front

What condition does binary search require?

Back

Binary search repeatedly halves the search range and typically runs in O(log n), but the searched array must already be sorted.

10
Front

Why cast an array sum to `double` when computing an average?

Back

Cast the sum to `double` before division, for example `(double) sum / scores.length`, so integer division does not discard the fractional part.

11
Front

How should minimum and maximum computations be initialized?

Back

Initialize both values from the first element, such as `values[0]`, then compare the remaining elements. This requires a nonempty array.

12
Front

How can you preserve an array before modifying it?

Back

Arrays are mutable: a method can change the caller's array object. Use `Arrays.copyOf` first when the original values must be preserved.