ArrayList Hacks

Hack 1

interface Turtle {
}
ArrayList<Boolean> booleans = new ArrayList<Boolean>();

ArrayList<Turtle> turtles = new ArrayList<Turtle>();

ArrayList<String> strings = new ArrayList<String>(10);

Hack 2

Choose 3 different methods from above to change around this sample ArrayList:

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.add(2);
        randomNumbers.remove(1);
        randomNumbers.set(0,2);
        System.out.println("ArrayList: " + randomNumbers);
    }
}
Hack2.main(null);
ArrayList: [1, 4, 7, 12, 23]
ArrayList: [2, 7, 12, 23, 2]

Hack 3

Here is some sample code for the total sum of integers. Finish the loop!

public class Hack3 {
    public static void main(String[] args) {
        ArrayList<Integer> values = new ArrayList<Integer>();
        values.add(1);
        values.add(4);
        values.add(7);
        values.add(12);
        values.add(23);
        System.out.println("ArrayList: " + values);
        
        int total = 0;

            for (int i=0; i < values.size(); i++) {
                total+=values.get(i);
            }

        System.out.print(total);
    }
}
Hack3.main(null);
ArrayList: [1, 4, 7, 12, 23]
47

Hack 4

Complete the Selection sort sample code by writing code for a swap of elements.

int [] arr = {5, 6, 24, 8, 9, 19, 44, 3};

for (int i = 0; i < arr.length; i++) {
 
    // nested loop 1 index ahead
    for (int j = i + 1; j < arr.length; j++) {

        // comparing elements
        int temp = 0;
        if (arr[j] < arr[i]) {

            temp = arr[j];
            arr[j]=arr[i];
            arr[i]=temp;
            
        }
    }

    // Printing sorted array 
    System.out.print(arr[i] + " ");
}
3 5 6 8 9 19 24 44 

Array and 2D Array Hacks

Hack 5

Make an array and iterate through it to only print the elements with even indexes (this includes 0). Then iterate through it again to only print the odd indexes.

int [] arr = {1, 3, 5, 7, 9, 11, 13, 15};

for(int i = 0; i < arr.length; i+=2){
    System.out.print(arr[i]+ " ");
}

System.out.println();

for(int i = 1; i < arr.length; i+=2){
    System.out.print(arr[i]+ " ");
}
1 5 9 13 
3 7 11 15 

Hack 6

Create a 2D array and iterate through it to print all of its elements.

int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
        System.out.print(arr[i][j] + " ");
    }
    System.out.println();
}
1 2 3 
4 5 6 
7 8 9 

Homework Part 1

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
import java.util.Arrays;
import java.util.Random;

public class SeatingChart {
    private String[][] chart;

    public SeatingChart(String[][] chart) {
        this.chart = chart;
    }

    public void alphabetize() {
        Arrays.sort(chart, (a, b) -> a[0].compareToIgnoreCase(b[0]));
    }

    public void shuffleSeats() {
        Random rand = new Random();
        for (int i = chart.length - 1; i > 0; i--) {
            for (int j = chart[i].length - 1; j > 0; j--) {
                int randomRow = rand.nextInt(i + 1);
                int randomCol = rand.nextInt(j + 1);
                String temp = chart[i][j];
                chart[i][j] = chart[randomRow][randomCol];
                chart[randomRow][randomCol] = temp;
            }
        }
    }

    public void addPerson(int row, int col, String name) {
        chart[row][col] = name;
    }

    public void deletePerson(int row, int col) {
        chart[row][col] = null;
    }

    public void printChart() {
        for (int i = 0; i < chart.length; i++) {
            for (int j = 0; j < chart[i].length; j++) {
                System.out.print(chart[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        String[][] chart = {{"Iris", "Sahil", "Ellen"}, {"Nathan", "Rohit", "Jun"}};
        SeatingChart seatingChart = new SeatingChart(chart);
        System.out.println("Original seating chart:");
        seatingChart.printChart();

        System.out.println("Alphabetized seating chart:");
        seatingChart.alphabetize();
        seatingChart.printChart();

        System.out.println("Shuffled seating chart:");
        seatingChart.shuffleSeats();
        seatingChart.printChart();

        System.out.println("Added person to row 0, col 2:");
        seatingChart.addPerson(0, 2, "Grace");
        seatingChart.printChart();
    }
}
SeatingChart.main(null);
Original seating chart:
Iris Sahil Ellen 
Nathan Rohit Jun 
Alphabetized seating chart:
Iris Sahil Ellen 
Nathan Rohit Jun 
Shuffled seating chart:
Iris Rohit Ellen 
Nathan Sahil Jun 
Added person to row 0, col 2:
Iris Rohit Grace 
Nathan Sahil Jun 

Extra credit

  • Complete a College Board FRQ involving arrays/ArrayLists

2018 Question 2 ArrayList/Arrays

// Part A
public WordPairList(String [] words){
    this.allPairs = new ArrayList<WordPair>();
    for(int i = 0; i < words.length -1; i++) {
        for(int j = i+1; j < words.length; j++) {
            WordPair newPair = new WordPair(words[i], words[j]);
            this.allPairs.add(newPair);
        }
    }
}
// Part B
public int numMatches() {
    int matches = 0;
    for(int i = 0; i < this.allPairs.size(); i++) {
        WordPair pair = this.allPairs.get(i);
        if(pair.getFirst().equals(pair.getSecond())) {
            matches ++;
        }
    }
    return matches;
}