Notes:

  • A 2D array is essentially an array of arrays. To elaborate, it is a type of multidimensional array.
  • To initialize a 2D array, specify the type, follow it with "[][]", then follow that with the name of the 2D array (e.g. int[][] numbers)
  • Method 1 of initializing 2D arrays: int[][] numbers1 = image.png
  • When you initialize a 2D array using method 1, each row can have a different number of columns, or in other words, each array within the array can have a different number of values.
  • Method 2 of initializing 2D arrays: int[][] numbers2 = new int[4][4];
  • Normally, to iterate through a 2D array, you use a for loop, which iterates through the rows, as well as a nested for loop, which iterates through the columns.
  • To access and change elements of 2D arrays, you would use indices to specify which row and which column.

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 Scanner library to take in user input
import java.util.Scanner;

// Class containing all of the 2D array methods
public class TwoDimensionalArrays {
    public int[][] numberList;
    public int[][] numberListReverse;
    Scanner userInput = new Scanner(System.in);
    
    // Constructor for initializing all of the randomly generated values in the 4 by 4 2D array
    public TwoDimensionalArrays() {
        numberList = new int[4][4];
        numberListReverse = new int[4][4];
        for (int a = 0; a < numberList.length; a++) {
            for (int b = 0; b < numberList[a].length; b++) {
                int randNum = (int) Math.floor(Math.random() * 101);
                numberList[a][b] = randNum;
                numberListReverse[a][b] = randNum;
            }
        }
    }

    // Method for printing a 2D array
    public void printArray(int[][] numberListPrint) {
        for (int x = 0; x < numberListPrint.length; x++) {
            for (int y = 0; y < numberListPrint[x].length; y++) {
                System.out.print(numberListPrint[x][y] + " ");
            }
            System.out.println("");
        }
    }

    // Method for reversing the rows and columns of the original 2D array
    public void reverseArray() {
        for (int i = 0; i < numberListReverse.length; i++) {
            for (int j = 0; j < (numberListReverse[i].length)/2; j++) {
                int numOfColumns = numberListReverse[i].length;
                int temp = numberListReverse[i][j];
                numberListReverse[i][j] = numberListReverse[i][numOfColumns - (j + 1)];
                numberListReverse[i][numOfColumns - (j + 1)] = temp;
            }
        }
        for (int i = 0; i < (numberListReverse.length)/2; i++) {
            int numOfRows = numberListReverse.length;
            int[] temp = numberListReverse[i];
            numberListReverse[i] = numberListReverse[numOfRows - (i + 1)];
            numberListReverse[numOfRows - (i + 1)] = temp;
        }
        printArray(numberListReverse);
    }

    // Method for getting the value of the original 2D array at the inputted coordinates
    public void getCorrespondingValue() {
        System.out.println("Choose the desired row (Starting with 0):");
        int rowNumber = userInput.nextInt();
        System.out.println("Choose the desired column (Starting with 0):");
        int columnNumber = userInput.nextInt();
        int correspondingValue = numberList[rowNumber][columnNumber];
        System.out.println("The corresponding value to the position you entered in the 2D array numberList is: " + correspondingValue);
    }

    // Method for multiplying each value of a row together, then adding all of the products together
    public void addProducts() {
        int sum = 0;
        for (int i = 0; i < numberList.length; i++) {
            int product = 1;
            for (int j = 0; j < numberList[i].length; j++) {
                product *= numberList[i][j];
            }
            sum += product;
        }
        System.out.println("The sum of all products in the original 2D array is: " + sum);
    }

}

// Main class that contains the main tester function
class Main {
    // Tester function to run all of the code outputs
    public static void main(String[] args) {
        // TwoDimensionalArrays object used for accessing and running the 2D array methods
        TwoDimensionalArrays myObj = new TwoDimensionalArrays();
        System.out.println("Original randomly generated 2D array:");
        System.out.println("-------------------------------------");
        myObj.printArray(myObj.numberList);
        System.out.println("");
        
        System.out.println("Reversed 2D array:");
        System.out.println("------------------");
        myObj.reverseArray();
        System.out.println("");

        System.out.println("Enter a position to get a corresponding value:");
        System.out.println("----------------------------------------------");
        myObj.getCorrespondingValue();
        System.out.println("");

        System.out.println("Sum of all row products of the original 2D array:");
        System.out.println("----------------------------------------------");
        myObj.addProducts();
        System.out.println("");
    }
}

Main.main(null);
Original randomly generated 2D array:
-------------------------------------
19 79 24 17 
96 84 91 18 
89 97 94 77 
13 59 85 81 

Reversed 2D array:
------------------
81 85 59 13 
77 94 97 89 
18 91 84 96 
17 24 79 19 

Enter a position to get a corresponding value:
----------------------------------------------
Choose the desired row (Starting with 0):
Choose the desired column (Starting with 0):
The corresponding value to the position you entered in the 2D array numberList is: 19

Sum of all row products of the original 2D array:
----------------------------------------------
The sum of all products in the original 2D array is: 81587689