Computer Science 111:

Introduction to Computer Science I

Chapter Eight

Gregory M. Kapfhammer


creative commons licensed ( BY-NC-SA ) flickr photo shared by Ed Yourdon

What is an Array?

Motivation for Using Arrays

Important Details

An array stores a list of values

The number corresponding to each position is the index

An array of size N is indexed from 0 to N-1

Refer to the example in Figure 8.1 on page 380!

Why do we start the index at the value of 0?

Let's Try It!

Run the git pull command!

Please compile and run the BasicArray.java

What is the output of this program?

Please check that it matches the output on page 382.

Can you explain how this program works?

Please refer to Figure 8.2 for more details

Array Bounds Checking

Ensures that the index is within the array

Why is this important to perform?

Let's avoid buffer overflow attacks!

Also, enables the detection of program defects

Can this be fully done at compile time?

What are the associated overheads?

Letter Counting

Run the git pull command in the "share" repository

Compile and run the LetterCount.java program

Run this program five times and check the output

Do you understand how it uses arrays and loops?

In the first for loop, change the < to <=

Re-compile and run your program! What happens?

Array Details

How do we declare an array?

int[] grades; and int grades[]; are both acceptable

Be consistent when you are implementing your programs!

How do we initialize an array to specific values?

int[] scores = {87, 98, 60, 80};

Arrays as Parameters

What method always accepts an array as a parameter?

An array as a parameter can be modified

You can change the contents of the array

You cannot make the array refer to another array

Arrays of Objects

Arrays can store primitives!

But, they can also store objects!

Let's try this with the GradeRange example

What output does this program produce?

What does the grades array look like in memory?

Please share your diagram with the class!

Command-Line Arguments

Are there other ways to give a program input?

Yes! You can input command-line arguments.

Did you ever notice that main has a parameter?

What is the purpose of String[] args?

Creating a NameTag

Review the source code for the NameTag.java program

How does this program work?

Please compile and run this program!

What output does this program produce?

What happens if you try to access args[2]?

Why does the program produce this type of output?

Two-Dimensional Arrays

Comparing 1D and 2D arrays

Can you draw a diagram to illustrate these?

Run the git pull command!

Find, compile, and run the TwoDArray.java program

What output does this program produce?

What if we change the value of 10 to something else?

Two-Dimensional Arrays

Dealing with "arrays of arrays"

Find, compile, and run the SodaSurvey.java program

What output does this program produce?

What is the meaning of the rows and columns?

What is the output of this program?

Make your own survey results and see how the output changes

Is this new output correct? How do you know?