What is a Java array?
An array is an ordered, fixed-size sequence whose elements all have the same type. Java arrays are objects.
Study 07 Arrays and Collections with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a Java array?
An array is an ordered, fixed-size sequence whose elements all have the same type. Java arrays are objects.
What is the valid index range of a Java array?
Java arrays use zero-based indexing: the first index is 0, and the last valid index is `array.length - 1`.
What is the difference between declaring and creating an array?
A declaration creates a variable that can refer to an array; `new` creates the array object with a specified length.
How do you obtain a Java array's length?
Use the field `array.length`, not the method call `array.length()`, to obtain an array's length.
How do you update one element of an array?
Assign a new value through its index, such as `scores[2] = 80;`. The assignment changes that existing element.
Why should an array loop use `i < array.length`?
An indexed loop should continue while `i < array.length`. Using `<=` attempts to access the invalid index `array.length`.
When is a for-each loop appropriate for an array?
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.
What is a linear search?
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).
What condition does binary search require?
Binary search repeatedly halves the search range and typically runs in O(log n), but the searched array must already be sorted.
Why cast an array sum to `double` when computing an average?
Cast the sum to `double` before division, for example `(double) sum / scores.length`, so integer division does not discard the fractional part.
How should minimum and maximum computations be initialized?
Initialize both values from the first element, such as `values[0]`, then compare the remaining elements. This requires a nonempty array.
How can you preserve an array before modifying it?
Arrays are mutable: a method can change the caller's array object. Use `Arrays.copyOf` first when the original values must be preserved.