More Dimensions
> Click or hit Control-Enter to run the code above
Next Few Weeks
-
The early deadline for MP0 was your deadline day this weekend.
-
MP0 is due next weekend on your deadline day.
-
Next week is our first midterm on imperative programming and concepts.
-
Working on MP0 is great preparation for our first midterm.
-
We will continue homework during midterm week as we begin to talk about object-oriented programming.
Questions?
Arrays Can Have Multiple Dimensions
// This is a single-dimensional array of ints of size 4
int[] samples = new int[4];
// This is a two-dimensional array of of chars of
// size 4 in the first dimension and 8 in the second
char[][] board = new char[4][8];
// This is a three-dimensional array of of doubles of
// size 6 in the first dimension, 8 in the second,
// and 10 in the third
double[][][] temperature = new double[6][8][10];
Understanding Multi-Dimensional Arrays
// This is a two-dimensional array
int[][] samples = new int[4][8];
// This is a one-dimensional array
int[] samplesSlice = samples[0];
> Click or hit Control-Enter to run the code above
Multi-Dimensional Array Initialization
We can also initialize multi-dimensional arrays:
int[][] measurements = {
{ 1, 2 },
{ 3, 4 }
};
// This is equivalent to
int measurements = new int[2][2];
measurements[0][0] = 1;
measurements[0][1] = 2;
measurements[1][0] = 3;
measurements[1][1] = 4;
(The ordering can be confusing, but you won’t see this often.)
> Click or hit Control-Enter to run the code above
Forget About Rows and Columns
(These aren’t spreadsheets—you’re not in business school.)
You’re In Charge
The first, second, third, or Nth index of an array mean whatever you want.
This is particularly important when representing certain kinds of data using multi-dimensional arrays.
Multi-Dimensional Data
What kind of data would work well in a multi-dimensional array?
Multi-Dimensional Data
What kind of data would work well in a multi-dimensional array?
-
Pictures and images: each pixel is stored in a 2-dimensional grid
-
Higher-dimensional data: our world is 3D—4D, actually—so our data should be as well.
-
Sound: even data that might seem one dimensional often turns out to be more complicated than we expected
Multi-Level Loops
A frequent use of nested for
loops is to iterate over multi-dimensional
arrays:
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
> Click or hit Control-Enter to run the code above
Testing
Test cases are an indispensable part of modern software development.
-
To perform a simple test, pick a input that you know the answer to and check that your function returns the expected value.
-
Try to pick cases that are hard and where you might not get the right answer—we call these corner cases.
String Search
Given a String
containing multiple words, determine if it contains a
particular word
First, what is our algorithm?
-
Split the input
String
into multiple words, which requires picking a delimiter. -
Examine each word in the resulting array and see if it is equal to the word that we are searching for
String Equality
How do we determine if two Strings are equal?
> Click or hit Control-Enter to run the code above
Object Equality
Strings in Java are objects.
Never test object equality using ==
!
Instead, using the .equals
method.
> Click or hit Control-Enter to run the code above
String Manipulation
Given a phone number formatted like "111.222.3333", convert it to "(111) 222-3333".
First, what is our algorithm?
-
Divide the input at each "."
-
Combine the values appropriately to create the new string
> Click or hit Control-Enter to run the code above
String Rotation
Given a string rotate it right by a certain amount.
-
CS125
rotated right by 1 becomes5CS12
-
CS125
rotated right by 2 becomes25CS1
-
CS125
rotated right by 5 becomesCS125
-
CS125
rotated right by 6 becomes5CS12
String Rotation
First, what is our algorithm?
> Click or hit Control-Enter to run the code above
Announcements
-
checkstyle
is fully enabled for this week’s quiz. You will receive no credit for incorrectly-formatted code.