Computer Science 111:

Introduction to Computer Science I

Gregory M. Kapfhammer


flickr photo shared by Yelp.com under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

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

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?


flickr photo shared by cursedthing under a Creative Commons ( BY-ND ) license

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?

2D Arrays

Comparing 1D and 2D arrays

Let's draw a diagram to illustrate these

Now, 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?


flickr photo shared by kjarrett under a Creative Commons ( BY ) license

2D 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. Does the output change?


flickr photo shared by kjarrett under a Creative Commons ( BY ) license

Any questions about arrays?