Vocab

Nested For Loops

  • A for loop inside of another for loop. These are used to loop through all the elements in a 2d array. One loop can work through the rows and the other the columns.

Out Of Bounds Error

  • happens when a loop goes beyond the last valid index in an array.

Initializing a 2D Array

  • Have to give it a value or give it a size
  • There are two ways
int [][] numbers1 = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int[][] numbers2 = new int[4][3];

Iteration

  • To iterate through a normal array you could use a for loop
for(int i = 0;i<alphabet.length;i++){
    for(int j = 0; j < alphabet[i].length;j++){ //nested for loops
        System.out.print(alphabet[i][j]+" ");
    }
    System.out.println(" ");
}

Accessing and Changing Elements of 2D Arrays

  • Can access using indexes
int[][] numbers = new int[2][2]; //method 2: Creates array with two rows and two columns
numbers[0][0] = 1;
numbers[0][1] = 4;
numbers[1][0] = 9;
numbers[1][1] = 16;

HW

import java.util.Scanner;

public class Arrays{
    int[][] numbers = {{5,1,3,4},{3,8,7,6},{9,12,14,13}};
    public void printNum() {
        System.out.println("\nForwards");
        for(int i = 0; i < numbers.length; i++){
            for(int j = 0; j < numbers[i].length; j++){
                System.out.print(numbers[i][j] + " ");
            }

            System.out.println();

        }
    }

    public void reverse() {
        System.out.println("\nReverse");
        for (int i = numbers.length - 1; i >= 0; i--){
            for(int j = numbers[i].length - 1; j >= 0; j--) {
                System.out.print(numbers[i][j]+ " ");
            }

            System.out.println();
        }
}

    public void userInput(){
        System.out.println("\nInput");
        Scanner scanner = new Scanner(System.in);

        System.out.print("Row: ");
        int rowIndex = scanner.nextInt();
        System.out.println(rowIndex);

        System.out.print("Column: ");
        int columnIndex = scanner.nextInt();
        System.out.println(columnIndex);

        System.out.println("Result = " + numbers[rowIndex][columnIndex]);

    }

    public void sumOfProducts(){
        System.out.println("\nSum of Row Products");
        int a = 1;
        int b = 0;

        for (int j = 0; j < numbers.length; j++) {
            for(int i = 0; i < numbers.length; i++){
                a = a*numbers[j][i];
            }
        
            b = b + a;
            System.out.println("product of row " + j + " = " + a);
            a = 1;
        }
        System.out.print("sum of rows = " + b);
    }

    public static void main(String[] args){
        Arrays array = new Arrays();
        array.printNum();
        array.reverse();
        array.userInput();
        array.sumOfProducts();
    }
}
Arrays.main(null);
Forwards
5 1 3 4 
3 8 7 6 
9 12 14 13 

Reverse
13 14 12 9 
6 7 8 3 
4 3 1 5 

Input
Row: 1
Column: 2
Result = 7

Sum of Row Products
product of row 0 = 15
product of row 1 = 168
product of row 2 = 1512
sum of rows = 1695