RMR Lessons
RMR Lessons
Unit 6
Lists
- mutable (changeable content)
- ordered (specific order by index)
- repeating values (can have repeating values)
- fixed sizes (size of array cannot be changed once initialized, can't add element)
Syntax
Uses .length for length, use curly brackets for assignment or [i] for specific index (both access or modifying)
Hacks
Hack 1
// create ArrayLists that satisfy the following
// a) that stores Boolean values
boolean[] bool = {true, false, true, true};
// b) that stores Turtle Objects
public class Turtle{}
Turtle[] turtles = {new Turtle(), new Turtle(), new Turtle(), new Turtle()};
// c) that initializes with 10 Strings
String[] strings = {"1", "2", "3", "4", "5", "1", "2", "3", "4", "5"};
import java.util.ArrayList;
public class Hack2 {
public static void main(Integer[] args) {
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
randomNumbers.add(1);
randomNumbers.add(4);
randomNumbers.add(7);
randomNumbers.add(12);
randomNumbers.add(23);
System.out.println("ArrayList: " + randomNumbers);
randomNumbers.remove(2);
randomNumbers.add(4);
Collections.sort(randomNumbers);
System.out.println("ArrayList: " + randomNumbers);
}
}
Hack2.main(null);
public class Hack3 {
public static void main(ArrayList<Integer> values) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(4);
numbers.add(7);
numbers.add(12);
numbers.add(23);
System.out.println("ArrayList: " + numbers);
int total = 0;
for (int i=0; i < numbers.size(); i++) {
total += numbers.get(i);
}
System.out.println("Total: " + total);
}
}
Hack3.main(null);
int[] array = {1, 23, 5, 3, 7};
for (int i = 0; i < array.length; i += 2){
System.out.println(array[i]);
}
int[][] array = {{1, 23, 5, 3, 7}, {5, 2, 8, 11, 14}, {2, 3, 4, 5, 6}};
for (int[] i : array){
for(int j : i){
System.out.print(Integer.toString(j) + " ");
}
System.out.println();
}
public class Test1
{
public static void main(String[] args)
{
int[][] array = { {1,2,3},{-1,-2,-3},{4,5,6} };
int sum = 0;
//ADD CODE HERE
for (int i = 0; i < array.length; i++){
sum += array[i][i];
}
System.out.println(sum);
}
}
Test1.main(null);
// code here
public boolean double23(int[] nums) {
for(int num1 : nums){
int counter = 0;
for(int num2 : nums){
if(num1 == 1){
break;
}
if(num1 == num2){
counter++;
}
if(counter >= 2){
return true;
}
}
}
return false;
}
public class Smth {
public static int find(int[][] arr, int n) {
int count = 0;
for(int[] row : arr) {
for (int num : row) {
if (num == n){
count++;
}
}
}
return count;
}
}
Smth.find(new int[][]{{1,2,3}, {1,2,2}}, 2);
Homework
Write a seating chart program for your group. Meet the following requirements:
A 2D array, array, or ArrayList A minimum of 3 methods to manipulate the seating chart (ex. alphabetize, shuffle seats, add/replace/delete people) Print results You will be graded on:
Completion of at least 5 of the above hacks (you can complete these during the in-class activity) Completion of the homework Extra credit if you complete a College Board FRQ involving arrays/ArrayLists Complete the hacks/homework on your personal fastpages.
ArrayList<String> seatingChart = new ArrayList<String>(List.of("Bailey", "Hetvi", "Iris", "Sahil", "Evan", "Everitt", "Samuel", "Don", "Rohit", "Jun", "Ellen", "Nathan"));
public class SeatingChart{
public static void print(){
for (String person : seatingChart){
System.out.println(person);
}
}
public static void reset(){
seatingChart = new ArrayList<String>(List.of("Bailey", "Hetvi", "Iris", "Sahil", "Evan", "Everitt", "Samuel", "Don", "Rohit", "Jun", "Ellen", "Nathan"));
}
public static void alphabetize(){
for (int j = 0; j < seatingChart.size()-1; j++){
int min_idx = j;
for (int i = j + 1; i < seatingChart.size(); i++){
if(seatingChart.get(min_idx).compareTo(seatingChart.get(i)) > 0){
min_idx = i;
}
}
String temp = seatingChart.get(min_idx);
seatingChart.set(min_idx, seatingChart.get(j));
seatingChart.set(j, temp);
}
print();
reset();
}
public static void shuffleSeats(){
ArrayList<String> temp = new ArrayList<>(List.of("Bailey", "Hetvi", "Iris", "Sahil", "Evan", "Everitt", "Samuel", "Don", "Rohit", "Jun", "Ellen", "Nathan"));
for(int i = 0; i < seatingChart.size(); i++){
int index = ThreadLocalRandom.current().nextInt(0, temp.size());
seatingChart.set(i, temp.get(index));
temp.remove(temp.get(index));
}
print();
reset();
}
public static void changeSeatingChart(String action, String person){
if(action.equals("add")){
seatingChart.add(person);
print();
reset();
} else if (action.equals("remove")) {
seatingChart.remove(person);
print();
reset();
} else {
System.out.println("invalid action");
}
}
public static void main(String[] args){
System.out.println("-------------Original------------");
print();
System.out.println("-------------Alphabetize------------");
alphabetize();
System.out.println("-------------Shuffle Seats------------");
shuffleSeats();
System.out.println("-------------Change Seating Chart (add) ------------");
changeSeatingChart("add", "Mr. M");
}
}
SeatingChart.main(null);
System.out.println(3 + 2 * 3);