class: center, middle, title title: Arrays
Task: Send an email to each student
Dear {{name}},
You are awesome! Welcome to class.
Best,
Tech Elevator
Given a function that performs the actual send:
void sendEmail(String name)
String ben = "Ben Haskell";
String jack = "Jack Skellington";
String leela = "Turanga Leela";
sendEmail(ben);
sendEmail(jack);
sendEmail(leela);
String student1 = "Ben Haskell";
String student2 = "Jack Skellington";
String student3 = "Turanga Leela";
sendEmail(student1);
sendEmail(student2);
sendEmail(student3);
String[] names = new String[3];
names[0] = "Ben Haskell";
names[1] = "Jack Skellington";
names[2] = "Turanga Leela";
for (String name : names) {
sendEmail(name);
}
- An array holds an ordered list of elements
- Each element of an array has the same type
- Arrays have a fixed length that cannot be changed
???
- Other data structures allow you to change some of these constraints
- Declaring an array variable by appending
[]
to the name
String names[];
int scores[];
double sum, readings[];
- Often initialized at the same time:
String names[] = new String[3];
int scores[] = new int[1000];
double sum, readings[] = new double[10];
???
- can be interspersed with non-array
- Arrays are indexed from
0
- Put the index between square brackets after the variable name
String[] names = new String[3];
// Set values of elements:
// Note: 0, 1, 2 (not 1, 2, 3)
names[0] = "Ben Haskell";
names[1] = "Jack Skellington";
names[2] = "Turanga Leela";
// Index doesn't have to be a constant
for (int i = 0; i < names.length; i++) {
sendEmail(names[i]);
}
???
- also called "offset", since it's the distance from the start
- Entire array can be initialized with special syntax
String[] names = new String[3];
names[0] = "Ben Haskell";
names[1] = "Jack Skellington";
names[2] = "Turanga Leela";
// Equivalent:
String[] names = {"Ben Haskell", "Jack Skellington", "Turanga Leela"};
int scores[] = new int[3];
scores[0] = 20;
scores[1] = 30;
scores[2] = 40;
// Equivalent:
int scores[] = {20, 30, 40};
- If you pass an array to a function, it can alter its contents
void modifyArray(String[] array, int i, String newValue) {
array[i] = newValue;
}
// ...
String[] letters = {"Alpha", "Bravo", "Charlie"};
modifyArray(letters, 2, "Chaplin");
System.out.println(letters[2]);
// => Chaplin