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.

2D Arrays

  • array of arrays
  • multidemensional, as many rows and colomns as you want

Initializing

  • created with 2 brackets (ex: int[][] numbers)
  • can be initialized by giving values or a size
    • if initialzed with values, each row can have a different number of colomns

Iterating

  • use for loop inside a for loop
  • first loops iterates through rows and second loop iterates through the colomnns

Acessing

  • use indexes to access values inside array
  • [1][2] is second row, third colomn

HW

  • Create a class for 2D array learning.
  • Create a method to 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.Scanner;

public class Array{
    int[][] numbers = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
    public void print(){
        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() {
        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 askIndex() {
        Scanner scanner = new Scanner(System.in);
    
        System.out.print("What row do you want? ");
        int rowIndex = scanner.nextInt();
        System.out.println(rowIndex);
    
        System.out.print("What col do you want? ");
        int colIndex = scanner.nextInt();
        System.out.println(colIndex);
    
        System.out.println(numbers[rowIndex][colIndex]);
    }

    public void multSum() { 
        int a = 1;
        int b = 0;

        for(int i = 0; i < numbers.length; i++){
            for(int j = 0; j < numbers[i].length; j++){
                a = a * numbers[i][j];
            }
            b = b + a;
            a = 1;
        }
        System.out.println("result: "+ b);
    }

    public static void main(String[] args){
        Array array = new Array();
        array.print();
        System.out.println();
        array.reverse();
        System.out.println();
        array.askIndex();
        System.out.println();
        array.multSum();
    }
}
Array.main(null);
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16 

16 15 14 13 
12 11 10 9 
8 7 6 5 
4 3 2 1 

What row do you want? 2
What col do you want? 3
12

result: 57264