Gregory M. Kapfhammer
Key Concept
Corresponding Diagram
In-Class Discussion
In-Class Activity
Details in the Textbook
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?
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
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?
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};
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 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!
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!
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
?
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?
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?