CSCE145 F2026 04 Arrays Part 01
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.
Transcript excerpt
0:01 Hey everyone, how goes it? Welcome back to computer science 145. Today, we're going to discuss arrays. Arrays are a very special type of structure. They are a collection of variables all of the same type. Arrays are one of the most foundational data structures and it's found in most programming languages. You can think of an array as being a contiguous block of memory. The size of an array must be specified initially, again, as it's a contiguous block of memory. But also, arrays cannot be resized. Now, Java treats arrays in a very special way. They are treated as an object type.
0:50 Now, just like other object types, the identifier of an array only holds a memory address that references the contents. You can think of the reference as it pointing to those contents. So, this also means that the double equals, in other words, checking if the values are equals, do not check the contents of the array. It only checks the memory address. Now, as we can see over here, this is the syntax to create an array. We use these square brackets in order to indicate that this is going to be an array of this type. We declare an array much like we declare
1:39 other variables, but also remember to include those square brackets. In order to initialize the array, in other words, create it, we have to use the reserved word new followed by the type again, then the square brackets where we have to specify the size right here. Again, arrays are contiguous blocks of memory of the same type. They cannot be resized. You have to initially give it the size in order to create that array. Now, just like other variables, whenever we declare it, we can also assign it at
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.