CS 145 Lecture 29 – ArrayList and the So Far Pattern
Agenda
- what ?s
- what does this do?
- arrays as collections
- the so-far pattern
What Does This Do?
ArrayList people = new ArrayList();
Random g = new Random();
people.add(g.nextPerson());
people.add(g.nextPerson());
people.add(g.nextPerson());
people.add(g.nextPerson());
people.add(g.nextPerson());
System.out.println(people.size());
System.out.println(people.get(1).getName());
people.get(4).jump(3);
people.get(0).highFive(people.get(3));
people.add(people.get(2));
people.remove(2);
int sum = 0; for (int i = 0; i < people.size(); ++i) { sum += people.get(i).getSiblings().size(); } System.out.println(sum);
Person winner = people.get(0); for (int i = 1; i < people.size(); ++i) { if (people.get(i).getDateOfBirth().isBefore(winner.getDateOfBirth())) { winner = people.get(i); } } System.out.println(winner.getName());
Person tmp = people.get(2); people.set(2, people.get(1)); people.set(1, tmp);
for (int i = 0; i < people.size(); ++i) { for (int j = 0; j < people.size(); ++j) { if (i != j) { people.get(i).shakeHands(people.get(j)); } } }
people.clear();
For-each/Map
make new array same size as old for each element in old transform element drop it in the corresponding location in the new array
So Far/Fold/Reduce
initialize so-far for each element in array combine element with so-far
Code
Sort.java
package lecture1110;
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
double[] times = {22.183333, 22.25, 21.6, 21.883333, 22.2, 22.5, 17.616667, 21.4, 20.716667, 22.166667, 21.233333, 21.183333, 18.85, 19.85, 22.1, 20.533333, 21.566667, 22.466667, 20.95, 22.033333, 21.7, 20.516667, 21.35, 21.233333, 21.233333, 21.466667, 21.066667, 21.416667, 19.483333, 20.016667, 21.016667, 20.25, 22.433333, 20.133333, 20.516667, 22.483333, 22.466667, 18.466667, 16.783333, 21.433333, 20.7, 22.516667, 22.4, 19.466667, 21.466667, 17.233333, 20.933333, 22.35, 20.216667, 19.55, 16.9, 18.333333, 20.633333, 21.016667, 20.6, 21.466667, 18.5, 22.383333, 21.566667, 22.483333, 21.433333, 21.433333, 21.683333, 20.783333, 22.433333, 20.85, 22.533333, 20.0, 21.45, 21.816667, 17.516667, 21.033333, 20.05, 22.066667, 18.366667, 20.816667, 21.5, 22.016667, 19.633333, 21.933333, 21.35, 21.716667, 20.216667, 18.35, 21.916667, 21.433333, 22.35, 18.95, 18.55, 21.15, 18.183333, 19.6, 20.833333, 21.05, 21.466667, 21.05, 21.866667, 21.616667, 16.533333, 21.366667};
Arrays.sort(times);
System.out.println(Arrays.toString(times));
}
}
Deck.java
package lecture1110;
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
public static void main(String[] args) {
String[] suits = {"\u2665", "\u2666", "\u2660", "\u2663"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
ArrayList<String> deck = new ArrayList<String>();
for (String suit : suits) {
for (String rank : ranks) {
deck.add(rank + suit);
}
}
Collections.shuffle(deck);
System.out.println(deck);
// for (int i = 0; i < suits.length; ++i) {
// String suit = suits[i];
}
}
SoFar.java
package lecture1110;
public class SoFar {
public static void main(String[] args) {
int[] numbers = {4, 8, 15, 16, 42, 5, 312, 2000000000};
System.out.println(sum(numbers));
System.out.println(max(numbers));
}
public static int sum(int[] numbers) {
int sumSoFar = 0;
for (int number : numbers) {
sumSoFar += number;
}
return sumSoFar;
}
public static int max(int[] numbers) {
int maxSoFar = numbers[0];
for (int i = 1; i < numbers.length; ++i) {
if (maxSoFar < numbers[i]) {
maxSoFar = numbers[i];
}
}
return maxSoFar;
}
}
Haiku
To rule the world
You only need two numbers
Yours big and theirs small
You only need two numbers
Yours big and theirs small