Skip to main content

Section 3.5 Arrays

As was said at the outset of this section, we are going to use Java ArrayLists because they are easier to use and more closely match the way that Python lists behave. However, if you look at Java code on the internet or even in your core Java books you are going to see examples of something called an array. In fact, you have already seen one example of an array declared in the ‘Hello World’ program. Listing 3.5.1 is a rewritten version of Listing 3.4.4 that uses primitive arrays rather than array lists.
Listing 3.5.1.
The main difference between Listing 3.5.1 and Listing 3.4.4 is that we declare count to be an Array of integers. We also can initialize short arrays directly using the syntax shown here: Integer[] count = {0,0,0,0,0,0,0,0,0,0}Then notice that we can use the square bracket notation count[idx] to index into an array.

Checkpoint 3.5.2.

Construct a short Java program that creates an array of three integers, changes the first value, and prints it. Drag the blocks into the correct order on the right.
You have attempted of activities on this page.