If you'd like, you can follow along with the lessons at this link.

Clone this repo (git clone https://github.com/Rebecca-123/rmr-tri3) and work on the hacks below. You can transfer these hacks to your personal fastpages once you're finished.

ArrayList Hacks

Hack 1

import java.util.ArrayList;
// create ArrayLists that satisfy the following
// a) that stores Boolean values
ArrayList<Boolean> boolarray = new ArrayList<Boolean>(3);
boolarray.add(true);
boolarray.add(false);
boolarray.add(true);
System.out.println(boolarray);

// b) that stores Turtle Objects
ArrayList<String> turtlearray = new ArrayList<String>(2);

// c) that initializes with 10 Strings
ArrayList<String> stringarray = new ArrayList<String>(10);
for (int i = 0; i<10; i++) {
    stringarray.add("string " + i);
}
System.out.println(stringarray);
[true, false, true]
[string 0, string 1, string 2, string 3, string 4, string 5, string 6, string 7, string 8, string 9]

Hack 2

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

import java.util.ArrayList;
import java.lang.Math;

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);
        ArrayList<Integer> a = randomNumbers;
        int c = (int)(Math.random()*10);
        a.set(0, c);
        int d = (int)(Math.random()*10);
        a.set(4, d);
        System.out.println(randomNumbers);
        ;
        ;
    }
}
Hack2.main(null);
ArrayList: [1, 4, 7, 12, 23]
[4, 4, 7, 12, 3]

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> 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++) {
                ;
            }
    }
}
Hack3.main(null);

Hack 4

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

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]) {

            // write the swap code!
            ;
        }
    }

    // Printing sorted array 
    System.out.print(arr[i] + " ");
}

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.

// code here

Hack 6

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

// code here

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

        //ADD CODE HERE

    }
}

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) {
  
}

Hack 9

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

// code here

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.