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 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
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?
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?
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!
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
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?
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?
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?