What is a one-dimensional array?
A fixed-size object that stores multiple values of the same type, such as int, double, boolean, or String references.
Study Java One-Dimensional Array Algorithms with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a one-dimensional array?
A fixed-size object that stores multiple values of the same type, such as int, double, boolean, or String references.
What indices are valid for an array of length n?
For an array of length n, the valid indices are 0 through n - 1. Java arrays use zero-based indexing.
What does int[] scores; do?
Declaration creates a reference variable but not an array object. For example, int[] scores; declares scores without allocating its elements.
What are Java array defaults after new?
new int[5] creates five elements initialized to 0. Java also defaults double to 0.0, boolean to false, and reference types to null.
What is the length of int[] values = {4, 8, 15, 16, 23}?
The initializer list supplies five values, so values.length is 5. An initializer list creates the array and infers its length.
How do you modify one array element?
Use square brackets with an index, such as values[1] = 20, to modify one element. The assignment changes the array itself.
When should you use an indexed for loop?
Use an indexed for loop when the algorithm needs an element's position or must assign new values to the array.
Why cannot a for-each variable modify an array?
The loop variable receives a copy of each element, so assigning to it does not change the array. Use an indexed loop to modify array elements.
What does indexOf({12, 7, 19, 7, 10}, 7) return?
indexOf returns 1, the first index containing 7. It stops at the first match even though 7 appears again at index 3.
What does countEven({12, 7, 19, 4, 10}) return?
countEven returns 3 because 12, 4, and 10 are even. A counting algorithm starts at 0 and increments once per matching element.
What is the average of {12, 7, 19, 4, 10}?
The sum is 52 and the length is 5, so the average is 10.4. Use (double) total / values.length to avoid integer division.
What is the maximum of {-8, -3, -12}?
The maximum is -3. Initializing max with the first element works even when every array value is negative; starting max at 0 would be incorrect here.