Iterations and Repetition (2D Arrays)

  • An array inside of an array
  • Multi dimensional
  • iterating through an array to get values
int[][] numbers;
String[][] names;
char[][] letters;
float[][] floats;
double[][] doubles;
Object[][] objects;

Accessing Values Inside of an Array

  • int[][]products = new int [10][10]; //creating 2D Array
  • printing values up, down, left, right, upside down, badabing badaboong yadda yadda yes yes

Homework

  • Create a class for 2D array learning.
  • Create a method too set 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 AaditGupta {
    int[][] bananas = {{1,2,3}, {4,5,6}, {7,8,9}};

    public void regular(){
        System.out.println("Regular Array - Unaltered");
        for(int i = 0; bananas.length > i; i++){
            for(int j=0; j < bananas[i].length; j++){
                System.out.print(bananas[i][j]);
            }
            System.out.println(" ");
        }
        System.out.println(" ");
    }

    public void mixmix(){
        int[] a = bananas[0];
        int[] b = bananas[2];
        bananas[0] = b;
        bananas[2] = a;
        System.out.println("Array - Reversed");
        for(int i = 0; bananas.length > i; i++){
            for(int j=0; j < bananas[i].length; j++){
                System.out.print(bananas[i][j]);
            }
            System.out.println(" ");
        }
        bananas[2] = b;
        bananas[0] = a;
        System.out.println(" ");
    }
    
    public void input(){
        System.out.println("Array - Input display");
        Scanner myObj = new Scanner(System.in);  
        System.out.println("Enter row (integer)");
        int row = myObj.nextInt() - 1;  // Read user input
        System.out.println("Enter column (integer)");
        int column = myObj.nextInt() - 1;  // Read user input
        System.out.println("Row: " + (row+1) + " | Column: " + (column+1) + " | Number: " +bananas[row][column]);
        System.out.println(" ");
    }

    public void multiply(){
        System.out.println("Array - Sum of Products of Rows");
        int a = 0;
        int sum = 0;
        boolean first = true;
        for(int i=0; i < bananas.length; i++){
            for(int j=0; j < bananas[i].length; j++){
                if(first == true){
                    a += bananas[i][j];
                    first = false;
                }
                else{
                    a *= bananas[i][j];
                }
            }
            sum += a;
            a = 0;
            first = true;
        }
        // 6, 120, 504
        System.out.println("Sum = " + sum); // 630
    }

    public static void main(String[] args) {
        AaditGupta aaditGupta = new AaditGupta();
        aaditGupta.regular();
        aaditGupta.mixmix();
        aaditGupta.input();
        aaditGupta.multiply();
    }
}

AaditGupta.main(null);
Regular Array - Unaltered
123 
456 
789 
 
Array - Reversed
789 
456 
123 
 
Array - Input display
Enter row (integer)
Enter column (integer)
Row: 3 | Column: 3 | Number: 9
 
Array - Sum of Products of Rows
Sum = 630