πŸ““4.3: Array Creation & Access

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 4.3

✴✴✴ NEW UNIT/SECTION! ✴✴✴
Create a blank Java program to take your class notes in for the next few lessons.
Click on the collapsed heading below for GitHub instructions ‡

πŸ““ NOTES PROGRAM SETUP INSTRUCTIONS
  1. Go to the public template repository for our class: BWL-CS Java Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS2-Unit4PartA-Notes
  4. For the description, write: 1D Array data collections, using text files
  5. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub! πŸ“‚

  6. Now on your repository, click and select the Codespaces tab
  7. Click Create Codespace on main and wait for the environment to load, then you’re ready to code!
  8. πŸ“ Take notes in this Codespace during class, writing code & comments along with the instructor.

πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK! Codespaces are TEMPORARY editing environments, so you need to COMMIT changes properly in order to update the main repository for your program.

There are multiple steps to saving in GitHub Codespaces:

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Click the button on the LEFT menu
  3. Type a brief commit message at the top of the file that opens, for example: updated Main.java
  4. Click the small βœ”οΈ checkmark in the TOP RIGHT corner
  5. Click the button on the LEFT menu
  6. Finally you can close your Codespace!

Array Creation and Access

To keep track of 10 exam scores, we could declare 10 separate variables:

int score1, score2, score3, … , score10;

But what if we had 100 exam scores? That would be a lot of variables! Most programming languages have a simple data structure for a collection of related data that makes this easier. In many block-based programming languages like App Inventor and Scratch, this is called a list. In Java and many programming languages, this is called an array.

An array is a block of memory that stores a collection of data items (elements) of the same type under one name. Arrays are useful whenever you have many elements of data of the same type that you want to keep track of, but you don’t need to name each one. Instead, you use the array name and a number (called an index) for the position of an item in the array.

You can make arrays of int, double, String, and even classes that you have written, like Student.

Here’s a video that introduces the concept of an array and gives an example.


An array is like a row of small lockers, except that you can’t cram lots of stuff into it β€” you can only store one value in each locker.

A row of lockers

You can store a value in an array using an index (location in the array). An array index is like a locker number β€” it helps you find a particular place to store or retrieve something.


Index Basics

Arrays and lists in most programming languages start counting elements at 0. This means:

  • The first element is at index 0.
  • The second element is at index 1.
  • The last element is at index array.length - 1.

Example:

int[] scores = {90, 85, 78, 92};
System.out.println(scores[0]); // prints 90
System.out.println(scores[3]); // prints 92

If you try to use an index less than 0 or greater than array.length - 1, you will get an ArrayIndexOutOfBoundsException.


Activity: Image Array

Type this in your Codespace, press run, and experiment with the index variable:

public class ImageEx {
    public static void main(String[] args) {
        String[] images = {
            "cow.jpg", "kitten.jpg", "puppy.jpg", "pig.jpg", "reindeer.jpg"
        };

        ImageEx obj = new ImageEx();
        int index = 0; // change this to show a different image
        obj.printHTMLimage(images[index]);
    }

    public void printHTMLimage(String filename) {
        String baseURL = "https://raw.githubusercontent.com/bhoffman0/CSAwesome/master/_sources/Unit6-Arrays/6-1-images/";
        System.out.print("<img src=" + baseURL + filename + " width=500px />");
    }
}
  1. Change index to show the puppy image, then the reindeer.
  2. Try all images β€” what indices did you use?
  3. Replace index with a random number: (int)(Math.random() * images.length).

Coding Challenge: Countries Array

US Map

  1. Create 4 parallel arrays for:

    • Countries: China, Egypt, France, Germany, India, Japan, Kenya, Mexico, United Kingdom, United States
    • Capitals
    • Languages
    • Image filenames
  2. Add more entries for countries you are interested in.
  3. Pick a random index using Math.random() and array .length.
  4. Print the country, its capital, its language, and its image using that index.

Arrays of Objects

You can create arrays of objects just like you create arrays of primitives. You must call the constructor for each object when initializing the array.

Example:

ClassName[] array = new ClassName[size];
array[index] = new ClassName();
array[index].method();

Activity: Turtle Array

import java.awt.*;

public class TurtleArray {
    public static void main(String[] args) {
        World world = new World(300, 300);
        Turtle[] turtarray = new Turtle[2];
        turtarray[0] = new Turtle(world);
        turtarray[1] = new Turtle(world);
        turtarray[0].forward();
        turtarray[1].turnLeft();
        turtarray[1].forward();
        world.show(true);
    }
}

Task:

  1. Change the array size to 3.
  2. Add another turtle at index 2.
  3. Make the new turtle turn right and move forward.

Community Challenge: Array of Your Class

  1. Copy your class from Lesson 3.5.
  2. Create an array of 3 objects of your class.
  3. Initialize each element using your class constructor.
  4. Call the print method for each object.

Summary

  • (AP 4.3.A.1) Arrays store multiple values of the same type β€” primitives or object references.
  • (AP 4.3.A.2) The size is fixed at creation and accessed via .length.
  • (AP 4.3.A.3) When created with new, elements are initialized to default values (0, 0.0, false, or null).
  • (AP 4.3.A.4) Initializer lists can create and populate arrays in one step.
  • (AP 4.3.A.5) Square brackets [] are used to access or modify elements by index.
  • (AP 4.3.A.6) Valid indices are 0 to length - 1; accessing outside this range throws an error.

AP Practice

Example Problem:

public void mystery(int[] a, int i) {
    a[i] = a[i-1] * 2;
}

If array is {4, 10, 15}, mystery(array, 2) β†’ {4, 10, 20}.


Arrays Game

Try this array practice game. Click Arrays and guess which element will be printed. Turn on Labels if needed.


Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.