CPSC Lecture8
CPSC Lecture8
Introduction to Computation
May 25, 2009
Administrative Stuff
Assignment #3 is due by 11:59pm Wednesday.
Midterm exam #2 in class on Friday.
Administrative Stuff
Big reading in Big Java 3rd edition:
Chapter 1.1 through 1.8 (introduction)
Chapter 2.1 through 2.10 (using objects)
Chapter 3.1 through 3.8 (implementing classes)
Chapter 4 (data types)
Chapter 5.1 through 5.4 (decisions)
Chapter 6.1 through 6.5 (iteration) Friday and today
Chapter 7.1, 7.5, 7.6, 7.7 (arrays) today and Wednesday
Chapter 14.1, 14.3 (searching and sorting)
Chapter 8.1 through 8.9 (designing classes)
Chapter 9.1, 9.2, 9.3 (interfaces and polymorphism)
Chapter 10 (inheritance)
Chapter 2.11, 2.12 (simple graphics)
Chapter 9.5, 9.6, 9.7, 9.8, 10.9, 10.10 (event handling
and graphical user interfaces)
Administrative Stuff
Big reading in Big Java 2nd edition:
Chapter 1.1 through 1.8 (introduction)
Chapter 2.1 through 2.10 (using objects)
Chapter 3.1 through 3.8 (implementing classes)
Chapter 4 (data types)
Chapter 6.1 through 6.4 (decisions)
Chapter 7.1 through 7.5 (iteration) Friday and today
Chapter 8.1, 8.5, 8.6, 8.7 (arrays) today and Wednesday
Chapter 19.1, 19.3 (searching and sorting)
Chapter 9.1 through 9.9 (designing classes)
Chapter11.1, 11.2, 11.3 (interfaces and polymorphism)
Chapter 13 (inheritance)
Chapter 5.1, 5.2 (simple graphics)
Chapter 11.5, 12.1, 12.2, 12.3 (event handling
and graphical user interfaces)
Administrative Stuff
Important reminder:
Please note that in order to pass the course you must:
obtain an overall grade of at least 50%
obtain a grade of at least 50% on the final exam
obtain an overall grade of at least 50% on the combined lab and
assignment grades
If you fail to satisfy any of the above criteria, a grade no greater than
45% will be assigned in the course. The instructor reserves the right
to modify this grading scheme as necessary throughout the term.
If you skip enough labs and assignments, it won't matter how well you
do on the exams...you will fail the course.
From the previous lecture
Iteration - executing the same block of
instructions over and over
the 'while' loop
the 'for' loop
how to impress your drinking buddies with
a loop
Practice problem
Write a program that uses a while loop to simulate the
flipping of a coin one million times. Keep track of how many
times the coin lands heads up, and how many times it lands
tails up. Print the results.
Practice problem
public class CoinFlipper
{
public static void main (String[] args)
{
int heads = 0, tails = 0, count = 0;
double rnum;
while (count < 1000000)
{
rnum = Math.random();
count++;
if (rnum < 0.5)
{
heads++;
}
else
{
tails++;
}
}
System.out.println("Coin landed heads " + heads + " times");
System.out.println("Coin landed tails " + tails + " times");
}
}
Practice problem
public class CoinFlipperFor
{
public static void main (String[] args)
{
int heads = 0, tails = 0;
double rnum;
for (int count = 0; count < 1000000; count++)
{
rnum = Math.random();
if (rnum < 0.5)
{
heads++;
}
else
{
tails++;
}
}
System.out.println("Coin landed heads " + heads + " times");
System.out.println("Coin landed tails " + tails + " times");
}
}
More data collection
If you complete our second survey no later than
Friday, May 29, we'll give you additional quiz marks.
Again, it's a little safety net in case you have a bad
quiz day.
This survey is waiting for you now at
http://ls.olt.ubc.ca/index.php?sid=76476&lang=en
What we'll see today...
...if we don't run out of time
How to create and manage loops within loops (nested
loops)
How to organize related data as a collection of variables
of the same type (the array)
How to access the array elements with a loop
How to make a more complex collection of data (the two-
dimensional array)
How to use the nested loop with the 2D array
Here's a very simple for loop:
public class SimpleLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
System.out.println(i);
}
}
}
What does it do?
It prints the following:
1
2
3
Nested loops
Here's a very simple for loop:
public class SimpleLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
System.out.println(i);
}
}
}
But what if for every number below, I also wanted to
print that value times 2, and that value times 3, just
a little multiplication table, like this?
1 2 3
2 4 6
3 6 9
Nested loops
Here's a very simple for loop:
public class SimpleLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
System.out.println(i);
}
}
}
It's as if for every number that was printed by the loop
above
1 2 3
2 4 6
3 6 9
Nested loops
Here's a very simple for loop:
public class SimpleLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
System.out.println(i);
}
}
}
It's as if for every number that was printed by the loop
above we need another loop to print the numbers in the
row.
1 2 3
2 4 6
3 6 9
Nested loops
Here's a very simple for loop:
public class SimpleLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
System.out.println(i);
}
}
}
It's as if for every number that was printed by the loop
above we need another loop to print the numbers in the
row. How do we do that?
1 2 3
2 4 6
3 6 9
Nested loops
We put a loop inside a loop, like this:
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
1
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
2
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
2
1
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
2
1 2
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
3
1 2
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
3
1 2
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
3
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
4
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
1
4
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
1
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
2
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
2
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
1
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
1
1 2 3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
1
1 2 3
2
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
2
1 2 3
2
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
2
1 2 3
2
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
2
1 2 3
2 4
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
3
1 2 3
2 4
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
3
1 2 3
2 4
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
3
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
4
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
2
4
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
2
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
3
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
3
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
1
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
1
1 2 3
2 4 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
1
1 2 3
2 4 6
3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
2
1 2 3
2 4 6
3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
2
1 2 3
2 4 6
3
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
2
1 2 3
2 4 6
3 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
3
1 2 3
2 4 6
3 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
3
1 2 3
2 4 6
3 6
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
3
1 2 3
2 4 6
3 6 9
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
4
1 2 3
2 4 6
3 6 9
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
j
3
4
1 2 3
2 4 6
3 6 9
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
3
1 2 3
2 4 6
3 6 9
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
4
1 2 3
2 4 6
3 6 9
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
Nested loops
i
4
1 2 3
2 4 6
3 6 9
How does it work?
public class NestedLoop
{
public static void main (String[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
System.out.print((i * j) + " ");
}
System.out.println();
}
}
}
That's the end of the program.
Nested loops
1 2 3
2 4 6
3 6 9
Practice Problems
Create a static method called printbox which takes two
integer parameters, w and h, and prints a box that is
w asterisks (*) wide and h asterisks high. For
example, printbox(3,5) will print this:
***
***
***
***
***
Practice Problems
public class BoxTest
{
public static void main (String[] args)
{
printbox(3,5);
System.out.println();
printbox(5,3);
}
public static void printbox(int wide, int high)
{
for (int i = 1; i <= high; i++)
{
for (int j = 1; j <= wide; j++)
{
System.out.print('*');
}
System.out.println();
}
}
}
Practice Problems
Create a static method called printtriangle which takes
one integer parameter, h, and prints a triangle that's
h asterisks high, as well as h asterisks wide at its
base. For example, printtriangle(5) would print this:
*
**
***
****
*****
Practice Problems
public class TriangleTest
{
public static void main (String[] args)
{
printtriangle(3);
System.out.println();
printtriangle(5);
}
public static void printtriangle(int high)
{
int j;
for (int i = 1; i <= high; i++)
{
j = 1;
while (j <= i)
{
System.out.print('*');
j++;
}
System.out.println();
}
}
}
My Empire Strikes Back
Bubbas
route
my
route
How do I keep track of my sales?
Cans of pop sold
this month
185
92
370
485
209
128
84
151
32
563
Whats the gross income?
Whats the net profit?
Is Bubba stealing loonies?
How do I keep track of my sales?
Cans of pop sold
this month
185
92
370
485
209
128
84
151
32
563
In other words, how can I
organize the data above in
my computer so that I can
access it easily and do the
computations I need to do?
Heres the answer
Cans of pop sold
this month
185
92
370
485
209
128
84
151
32
563
We'll use an array. An array is a
common programming language
construct for grouping related data
items together in some meaningful
organization such that each individual
data item can be easily retrieved or
updated.
Heres the answer
cansSold
185
92
370
485
209
128
84
151
32
563
We'll use an array. An array is a
common programming language
construct for grouping related data
items together in some meaningful
organization such that each individual
data item can be easily retrieved or
updated.
You can think of an array as a
collection of variables, all of the same
type, that share a common name.
Each of these variables holds a single
value.
How to use the array
cansSold
185
92
370
485
209
128
84
151
32
563
Since this collection of variables has
a single name, how do we access
the individual values?
How to use the array
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Since this collection of variables has
a single name, how do we access
the individual values?
Each value is stored at a unique
numbered position within the array.
The number is commonly called the
index of that array element.
Sometimes that number is called a
subscript.
This array is named cansSold. It
holds 10 values.
How to use the array
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
To access an individual value in an
array, we use the array name followed
by a pair of square brackets. Inside
the brackets we place the index of the
array element we want to access. For
example
System.out.println(cansSold[4]);
would print the value 209 on the
monitor. This means of referring to
an array element can be used
anywhere a variable name can be
used.
Array declarations and types
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Just like an ordinary variable, we have
to declare an array before we use it,
and we have to give it a type. Since
cansSold contains integers, we make
it an integer array:
int[] cansSold = new int[10]
It looks like a variable declaration
except:
Array declarations and types
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Just like an ordinary variable, we have
to declare an array before we use it,
and we have to give it a type. Since
cansSold contains integers, we make
it an integer array:
int[] cansSold = new int[10]
It looks like a variable declaration
except:
the empty brackets on the left tell Java that
cansSold is an array...
Array declarations and types
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Just like an ordinary variable, we have
to declare an array before we use it,
and we have to give it a type. Since
cansSold contains integers, we make
it an integer array:
int[] cansSold = new int[10]
It looks like a variable declaration
except:
the empty brackets on the left tell Java that
cansSold is an array...
the number in the brackets on the right tell
Java that the array should have room for 10
elements when it's created
Array declarations and types
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Just like an ordinary variable, we have
to declare an array before we use it,
and we have to give it a type. Since
cansSold contains integers, we make
it an integer array:
int[10] cansSold = new int[10]
It looks like a variable declaration
except:
the empty brackets on the left tell Java that
cansSold is an array...
the number in the brackets on the right tell
Java that the array should have room for 10
elements when it's created
DO NOT put the size of the array in the
brackets on the left
Array declarations and types
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Just like an ordinary variable, we have
to declare an array before we use it,
and we have to give it a type. Since
cansSold contains integers, we make
it an integer array:
int[10] cansSold = new int[10]
It looks like a variable declaration
except:
the empty brackets on the left tell Java that
cansSold is an array...
the number in the brackets on the right tell
Java that the array should have room for 10
elements when it's created
DO NOT put the size of the array in the
brackets on the left
Array declarations and types
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Here's a simple example of how to create and initialize the
cansSold array:
public class ArrayTest1
{
public static void main(String[] args)
{
final int ARRAYSIZE = 10;
int[] cansSold = new int[ARRAYSIZE];
cansSold[0] = 185;
cansSold[1] = 92;
cansSold[2] = 370;
cansSold[3] = 485;
cansSold[4] = 209;
cansSold[5] = 128;
cansSold[6] = 84;
cansSold[7] = 151;
cansSold[8] = 32;
cansSold[9] = 563;
// do useful stuff here
System.out.println("Element 4 is " +
cansSold[4]);
}
}
Variation on creating an array
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
Here's another way to do the same thing. This is
called an initializer list.
public class ArrayTest2
{
public static void main(String[] args)
{
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
// do useful stuff here
System.out.println("Element 4 is " +
cansSold[4]);
}
}
Note that the right hand side of this declaration does
not include type or size. Java figures out the size by
itself. And the types of the values on the right have to
match the type declared on the left.
The initializer list may only be used when the array
is first declared.
So let's do something already...
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How would we write a program to create this array, find
the total of cans sold, and print the result?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
So let's do something already...
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
So let's do something already...
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
totalCans
0
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
totalCans
cansSold
0
cansSold.length
10
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
0
cansSold.length
10
0
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
0
cansSold.length
10
0
Is i < 10? Yes.
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
185
cansSold.length
10
0
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
185
cansSold.length
10
1
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
185
cansSold.length
10
1
Is i < 10? Yes.
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
277
cansSold.length
10
1
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
277
cansSold.length
10
2
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
277
cansSold.length
10
2
Is i < 10? Yes.
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
647
cansSold.length
10
2
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
647
cansSold.length
10
3
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
647
cansSold.length
10
3
Is i < 10? Yes.
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
1132
cansSold.length
10
3
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
1341
cansSold.length
10
4
And so on...
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
1469
cansSold.length
10
5
And so on...
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
1553
cansSold.length
10
6
And so on...
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
1704
cansSold.length
10
7
And so on...
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
1736
cansSold.length
10
8
And so on...
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
2299
cansSold.length
10
9
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
2299
cansSold.length
10
10
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
2299
cansSold.length
10
10
Is i < 10? No.
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
How does it work?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
2299
cansSold.length
10
10
"We've sold 2299 cans of pop" is printed
on the monitor
So let's do something already...
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
What would happen if we made this little change?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i <= cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
i
totalCans
cansSold
2299
cansSold.length
10
10
java.lang.ArrayIndexOutOfBoundsException: 10
Some programming languages don't catch this.
Something to remember
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
The array cansSold was created with
10 elements. The indices (plural of
index) are 0 through 9.
In general, an array of size n will have
indices ranging from 0 through n-1.
When you number things, you're used
to beginning with 1. Computer folks
begin with 0. This leads to "off by one"
errors, even among computer veterans.
10
cansSold.length
185
92
370
485
209
128
84
151
32
563
1
2
3
4
5
6
7
8
9
10
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
You'll always be mapping
your addressing system
onto the array's numbering
system, so pay close
attention to what you're
doing.
Something to remember
What if...
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
...we want to change this program so that it prints the
number of cans at each array index starting with the last
index and working up to the first index?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i <= cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
What if...
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
...we want to change this program so that it prints the
number of cans at each array index starting with the last
index and working up to the first index?
public class ArrayTest3a
{
public static void main(String[] args)
{
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = cansSold.length - 1; i >= 0; i--)
{
System.out.println(cansSold[i]);
}
}
}
What if...
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
...we want to change this program so that it computes and
prints the average of the number of cans sold per Coke
machine?
public class ArrayTest3
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i <= cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("We've sold " + totalCans
+ " cans of pop");
}
}
What if...
cansSold
0 185
1 92
2 370
3 485
4 209
5 128
6 84
7 151
8 32
9 563
...we want to change this program so that it computes and
prints the average of the number of cans sold per Coke
machine?
public class ArrayTest3b
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, 92, 370, 485, 209,
128, 84, 151, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("Average sales: " +
((double)totalCans/cansSold.length));
}
}
Note that we're assuming the length of the array is always
the same as the number of Coke machines to be used in
calculating the average. This assumption may be wrong.
What if...
cansSold
0 185
1 -1
2 370
3 485
4 209
5 128
6 84
7 -1
8 32
9 563
...dysfunctional machines are indicated by a negative
sales value in the array, so we only want to use non-
negative values when computing the average?
public class ArrayTest3b
{
public static void main(String[] args)
{
int totalCans = 0;
int[] cansSold = {185, -1, 370, 485, 209,
128, 84, -1, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
totalCans = totalCans + cansSold[i];
}
System.out.println("Average sales: " +
((double)totalCans/cansSold.length));
}
}
What if...
cansSold
0 185
1 -1
2 370
3 485
4 209
5 128
6 84
7 -1
8 32
9 563
...dysfunctional machines are indicated by a negative
sales value in the array, so we only want to use non-
negative values when computing the average?
public class ArrayTest3c
{
public static void main(String[] args)
{
int totalCans = 0, totalMachines = 0;
int[] cansSold = {185, -1, 370, 485, 209,
128, 84, -1, 32, 563};
for (int i = 0; i < cansSold.length; i++)
{
if (cansSold[i] >= 0)
{
totalCans = totalCans + cansSold[i];
totalMachines++;
}
// no else needed
}
System.out.println("Average sales: " +
((double)totalCans/totalMachines));
}
}
Can we have an array of arrays?
Of course we can!
0
1
2
3
0 0
1 0
2 0
0 0
1 1
2 2
0 0
1 2
2 4
0 0
1 3
2 6
But in any given array, all the data has to be of the same type.
And all the arrays in an array of arrays have to be of the same type.
So if that's what you need, it's easier to use a two-dimensional array...
The two-dimensional array
0
1
2
3
0 1 2
0 0 0
0 1 2
0 2 4
0 3 6
"rows"
"columns"
In Java, the two-dimensional array is actually
implemented internally as an array of arrays,
but externally the syntax of the two-dimensional
array may seem easier to use.
The typical control structure for computing with
a two-dimensional array is a nested loop --
a loop within another loop. (Note however that
earlier we used a nested loop with a one-
dimensional array...it's often the case that a
nested loop indicates a two-dimensional array or
vice versa, but it's not a given.)
How would you write a program to load this
array with the values shown, and then
print the contents of the array?
The two-dimensional array
0
1
2
3
0 1 2
0 0 0
0 1 2
0 2 4
0 3 6
public class ArrayTest5
{
public static void main(String[] args)
{
int[][] multTable = new int[4][3];
for (int row = 0; row < multTable.length; row++)
{
for (int col = 0; col < multTable[row].length; col++)
{
multTable[row][col] = row * col;
}
}
for (int row = 0; row < multTable.length; row++)
{
for (int col = 0; col < multTable[row].length; col++)
{
System.out.print(multTable[row][col] + " ");
}
System.out.println();
}
}
}