Unit 6

Lists

  1. mutable (changeable content)
  2. ordered (specific order by index)
  3. repeating values (can have repeating values)
  4. 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"};

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.remove(2);
        randomNumbers.add(4);
        Collections.sort(randomNumbers);

        System.out.println("ArrayList: " + randomNumbers);
    }
}
Hack2.main(null);
ArrayList: [1, 4, 7, 12, 23]
ArrayList: [1, 4, 4, 12, 23]

Hack 3

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

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);
ArrayList: [1, 4, 7, 12, 23]
Total: 47

Hack 5

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

Array/ArrayList 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[] array = {1, 23, 5, 3, 7};

for (int i = 0; i < array.length; i += 2){
    System.out.println(array[i]);
}
1
5
7

Hack 6

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

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();
}
1 23 5 3 7 
5 2 8 11 14 
2 3 4 5 6 

Hack 7

Find the sum of the values on the diagonal of the 2D array. Print the sum.

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);
5

Hack 8

Given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2.

double23([2, 2]) → true double23([3, 3]) → true double23([2, 3]) → false

// 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;
}

Hack 9

Write a class to count how many of a number n is in a 2d array int[][] x

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);
3

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);
-------------Original------------
Bailey
Hetvi
Iris
Sahil
Evan
Everitt
Samuel
Don
Rohit
Jun
Ellen
Nathan
-------------Alphabetize------------
Bailey
Don
Ellen
Evan
Everitt
Hetvi
Iris
Jun
Nathan
Rohit
Sahil
Samuel
-------------Shuffle Seats------------
Everitt
Iris
Ellen
Evan
Samuel
Sahil
Hetvi
Don
Nathan
Bailey
Jun
Rohit
-------------Change Seating Chart (add) ------------
Bailey
Hetvi
Iris
Sahil
Evan
Everitt
Samuel
Don
Rohit
Jun
Ellen
Nathan
Mr. M
System.out.println(3 + 2 * 3);
9