Computer Science 100

Computational Expression

Gregory M. Kapfhammer

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

What is an Array?

Motivation for Using Arrays

Examples of Prior Usefulness?

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 build and run the BasicArrayComputations.java

Add the required source code to this program

Answer all of the questions about this program

What is the output of this program?

Please check that it matches the output on page 358

Can you explain how this program works?

Please refer to Figure 8.2 for more details

Do you see the benefit of using arrays?

Any questions about this array-based computation?

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?

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!

Letter Counting

Refer to listing 8.3 in the textbook

How does this program use for loops?

How does does this program use arrays?

See page 364 for an example of the program's output!


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

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

See the source code in Listing 8.10 for more details

How does this program work?

What output does this program produce?

What would happen if you try to access args[2]?

Why would the program produce this type of output?

One-dimensional arrays

Two-dimensional arrays

Let's investigate these further!

2D Arrays

First, let's compare and contrast 1D and 2D arrays

What output does this program produce?

Let's draw a diagram to illustrate these

Review the source code for the TwoDArray.java program

See the source code in Listing 8.13 for more details

What output does this program produce?


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

Any questions about arrays?

What are the connections to prior material?

We will practice during upcoming assignments

Schedule office hours for further discussion

Consider applications for your final project