Notes

Initializing A 2D Array

  • Initialize by giving each slot a value
  • Initialize by giving each slot a size
  • int[][] nums = new int[rows][columns]
// Method 1 (value):
    int[][] numbers1 = {{1,2,3,4}, 
                        {5,6,7,8},
                        {9,10,11,12},
                        {13,14,15,16}};

    String[][] names1 = {{"John","James","Jay"},
                        {"Melissa","May","Maggie"},
                        {"Bob","Burt","Billy"}};

    char[][] letters1 = {{'a','b','c'},
                        {'d','e','f','g','h'}};

// Method 2 (size):
    // Array with four rows and 3 columns
    int[][] numbers2 = new int[4][3];

    // Array with 2 rows and 2 columns
    String[][] names2 = new String[2][2];
    
    // Array with 2 rows and 3 columns
    char[][] letters2 = new char[2][3];

Accessing and Changing Elements of 2D Array

  • Access elements using index of element
// 2D Array w/ 2 rows and 2 columns
int[][] numbers = new int[2][2];

// Setting elements in 2D Array
numbers[0][0] = 1;
numbers[0][1] = 4;
numbers[1][0] = 9;
numbers[1][1] = 16;

//  Looping through the array to print each element
for(int i = 0;i<numbers.length;i++) {
    for(int j = 0; j < numbers[i].length;j++) { //nested for loops
        System.out.print(numbers[i][j]+" ");
    }
    System.out.println(" ");
}
1 4  
9 16  

Homework

  • Create a class for 2D array learning.
  • Create a method too initialize a 2D array with arbitrary values
  • Create a method to reverse the 2D array and print out the values
  • Create a method that asks for the input of a position and it returns the corresponding value
  • Create a method that multiplies each value in a row and then adds all the products together
  • Create a new object to test out each method in the main function
import java.util.*;
import java.lang.Math.*;

// Create class
public class Learning {

    private int[][] arr = new int[3][3];
    private int[][] reversed_arr = new int[3][3];
    
    // Arbitrary values
    public void origArr() {
        System.out.println("\nOriginal array:");
        // Assign a random value to the 2D array and print it
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                // Assign a random value to the 2D array using math.random()
                arr[i][j] = (int) (Math.random() * 10);
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    // Reverse the 2D array
    public void reversedArray() {
        System.out.println("\nReversed array:");
        
        // Reverse the 2D array and print it
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                // Reverses array by going backwards through the original array
                reversed_arr[i][j] = arr[arr.length - 1 - i][arr[i].length - 1 - j];
                System.out.print(reversed_arr[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    // Multiplies each value in a row and then adds all the products together
    public int productSum() {
        int product = 1;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                // Multiply product by value at this array index
                product *= arr[i][j];
            }
            // Add product to sum after all in row were multiplied, then reset product back to 1
            sum += product;
            product = 1;
        }
        return sum;
    }

    // Input of a position and it returns the corresponding value
    public int valueOf(int a, int b) {
        // Try to get array value, return exception if can't
        try {
            return arr[a][b];
        }
        catch (Exception e) {
            System.out.println("Error: " + e);
            return -1;
        }
    }
    
    // Tester method
    public static void main(String[] args) {
        Learning l = new Learning();
        l.origArr(); l.reversedArray(); 
        System.out.println("Value of [1][1]: " + l.valueOf(1, 1));
        System.out.println("Sum of products of rows: " + l.productSum());
    
        Learning w = new Learning();
        w.origArr(); w.reversedArray();
        System.out.println("Value of [2][1]: " + w.valueOf(2, 1)); 
        System.out.println("Sum of products of rows: " + w.productSum());
    }
    
}

Learning.main(null);
Original array:
9 9 9 
7 4 2 
7 8 7 

Reversed array:
7 8 7 
2 4 7 
9 9 9 
Value of [1][1]: 4
Sum of products of rows: 1177

Original array:
3 2 7 
9 3 3 
6 3 2 

Reversed array:
2 3 6 
3 3 9 
7 2 3 
Value of [2][1]: 3
Sum of products of rows: 159

Extra Credit

class MonkeyLoop {
    String [][] monkeys;
    String [][] cthulhus;


    public MonkeyLoop() {
        monkeys = new String[][]{

                {
                        "ʕง ͠° ͟ل͜ ͡°)ʔ ",      //[0][0] eyes
                },

                {
                        "  \\_⏄_/  ",      //[1][0] chin

                },

                {
                        "  --0--   ",       //[2][0] body

                },

                {
                        "  ⎛   ⎞   "        //[3][0] legs
                },


        };

        cthulhus = new String[][]{
            {
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⡀⠀⠀⠀⠀⠀⠀⢎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ʕง ͠° ͟ل͜ ͡°)ʔ ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⢱⠀⠀⢀⣤⡀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀  \\_⏄_/  ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠻⠉⣧⣿⣿⣿⠀⠀⢸⠇⠀⠐⠉⡆⠀⠀⠀⠀⠀⠀  --0--   ",
                    "⠀⠀⠀⠀⢀⠔⠒⢦⠀⢻⡄⠀⢿⣻⣿⡿⢀⣴⣋⣄⣄⣌⣠⠃⠀⠀⠀⠀⠀⠀  ⎛   ⎞   ",
                    "⠀⠀⠀⠀⠈⠀⢀⡞⠀⠈⠛⣷⣾⣿⣿⣿⣿⣿⣯⣥⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠈⠷⣦⣴⡾⢿⣿⡿⢿⣿⣋⣽⠶⢦⠙⢷⡀⠀⠀⠀⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⡏⢀⡆⠈⠉⠁⡄⠈⡇⠘⢇⠀⢈⡆⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡯⠀⠸⠁⠀⠀⠸⣧⡀⡇⠀⠈⠉⠉⠀⠀⠀⠀⠀         ",
                    "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⡴⠁⠀⠀⠀⠀⠙⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀          "
            },
        };
    }

    public void printPoem() {
        System.out.println();
        System.out.println("A story of a Monkey and Cthulhus");
        int monkeyCount = monkeys.length;
// Missing code
        for (int i = 1; i >= 1; i--)
        {
            for (int row = 0; row < monkeyCount; row++) {  //cycles through "cells" of 2d array
                for (int col = 0; col < monkeys[row].length; col++) {
                    System.out.print(monkeys[row][col] + " ");
                }

                //this new line gives separation between each stanza
                System.out.println();
            }
        }
            //hint: missing code, another for loop
        int cthulhuCount = cthulhus[0].length;
        for (int col1 = 0; col1 < cthulhuCount; col1++) {  //cycles through "cells" of 2d array
        for (int row1 = 0; row1 < 1; row1++){
            System.out.print(cthulhus[row1][col1]);
        }
        // Spacer
        System.out.println();
        }
// End of missing code
        // System.out.println(cthulhuCount);
        System.out.println("0000000000000000000000000000000000");
        System.out.println("             THE END              ");
    }


    public static void main(String[] args)  {
        new MonkeyLoop().printPoem();
    }
}
MonkeyLoop.main(null);
A story of a Monkey and Cthulhus
ʕง ͠° ͟ل͜ ͡°)ʔ  
  \_⏄_/   
  --0--    
  ⎛   ⎞    
⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⡀⠀⠀⠀⠀⠀⠀⢎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ʕง ͠° ͟ل͜ ͡°)ʔ 
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⢱⠀⠀⢀⣤⡀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀  \_⏄_/  
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠻⠉⣧⣿⣿⣿⠀⠀⢸⠇⠀⠐⠉⡆⠀⠀⠀⠀⠀⠀  --0--   
⠀⠀⠀⠀⢀⠔⠒⢦⠀⢻⡄⠀⢿⣻⣿⡿⢀⣴⣋⣄⣄⣌⣠⠃⠀⠀⠀⠀⠀⠀  ⎛   ⎞   
⠀⠀⠀⠀⠈⠀⢀⡞⠀⠈⠛⣷⣾⣿⣿⣿⣿⣿⣯⣥⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠈⠷⣦⣴⡾⢿⣿⡿⢿⣿⣋⣽⠶⢦⠙⢷⡀⠀⠀⠀⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⡏⢀⡆⠈⠉⠁⡄⠈⡇⠘⢇⠀⢈⡆⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡯⠀⠸⠁⠀⠀⠸⣧⡀⡇⠀⠈⠉⠉⠀⠀⠀⠀⠀         
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⡴⠁⠀⠀⠀⠀⠙⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀          
0000000000000000000000000000000000
             THE END