CSCE145 F2026 04 Arrays Part 01
Watch on YouTube →
Overview
Java arrays group a fixed number of values of one type in an object accessed through a reference; zero-based indices provide constant-time element access, while `.length` and `for` loops support safe traversal. A Java console program applies these ideas to a five-contestant “closest without going over” game for Shaq Fu, using a `double[]`, input validation, and `-1` sentinel values to find a winner or report that no one won.
Key takeaways
- A Java array’s size is fixed at creation, and its variable holds a reference to the array object rather than the elements themselves.
- For an array of length `n`, valid indices run from 0 to `n - 1`; using `i < array.length` in a loop safely visits every element.
- Array indexing enables direct random access: the example models an integer element’s address as the starting address plus `index * 4` bytes.
- Java’s `==` compares array references, not array contents, so equal elements do not imply that two array variables refer to the same array.
- In the price game, scanning guesses once and retaining the greatest value at or below `$5.97` identifies the closest contestant without going over.
- The `-1` sentinel distinguishes “no qualifying guess found” from valid contestant indices, which range from 0 to 4.
Chapters
- An array stores a fixed-size collection of values of one type; Java treats arrays as objects and their identifiers hold references rather than the contents.
- Declare an array with brackets, then create it with `new` and a specified size, such as `new int[5]`; arrays cannot be resized.
- A declared but uninitialized array reference defaults to `null`; creating an `int[5]` allocates five integer elements, each initialized to zero.
- The memory illustration uses address 60 and four-byte integers to show how elements of the array occupy consecutive locations.
- Array indices start at 0, so a five-element array has valid indices 0 through 4; accessing an invalid index causes an index-out-of-bounds runtime exception.
- Read or modify an element with bracket notation, such as `array[0] = 1` and `array[4] = 5`.
- Java’s `==` on array references compares their memory addresses, not whether their elements have equal contents.
- Random access calculates an element’s location from the starting address and index; for integers in the example, the offset is `index * 4` bytes.
- Use the array’s `.length` field to get its size and `.length - 1` to identify the last valid index.
- A counting loop starting at `i = 0` and continuing while `i < array.length` visits every element without exceeding the array bounds.
- For a five-element integer array, the loop assigns indices 0 through 4, then stops when `i` becomes 5.
- The loop counter can serve directly as the index for reading or updating each array element.
- The example stores guesses from five contestants in a `double[] prices`, with `SIZE = 5` and the Shaq Fu game’s actual price set to `$5.97`.
- A `Scanner` reading `System.in` collects one guess per contestant in a loop over `prices.length`.
- Guesses below zero are rejected and stored as `0`; the program then prints the actual price before selecting a winner.
- The selection loop accepts guesses at or below `$5.97` and replaces the current closest guess when a larger valid price is found.
- Initializing `closestPrice` and `winnerIndex` to `-1` marks that no valid winning guess has been found yet.
- If every contestant guesses above `$5.97`, `winnerIndex` remains `-1` and the program reports “No one wins.”
- For guesses of `$13`, `$50`, `$4`, `$1.99`, and `$8.99`, contestant index 2 wins with `$4`; five guesses of `$10` produce no winner.
- The debugger shows the five `double` elements initialized to `0.0`, then tracks each input assignment and the winner update at index 2.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.