2d Array

  • Multi dimensional
  • 8-10% of the college board test
  • Traversing
  • How to call one

Creating A 2D Array

  • Instead of one set of brackets, you have 2 sets of brackets

Accessing and changing elements

  • Put indexes into the brackets
    • 2 dimensional aspect

Printing with for loop

  • Iterate through both i and j indexes with for loop
  • Can display values both backwards and upside down

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
int[][] nums = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

System.out.println("Reverse: ");
for(int i = nums.length-1; i >= 0; i--){
    for(int j = nums[i].length-1; j>=0; j--){
        System.out.println(nums[i][j]);
    }
}
Reverse: 
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
import java.util.Scanner;  

public class Arrays{
    int[][] nums;

    public Arrays(){
        int[][] tmp = new int[3][3];
        for (int i = 0; i < tmp.length; i++){
            for (int j = 0; j < tmp[i].length; j++){
                tmp[i][j] = (j+1) * (short) (Math.random()*11);
            }
        }
        this.nums = tmp;
    }

    public void print(){
        for(int i = 0; i < nums.length; i++){
            for(int j = 0; j < nums[i].length; j++){
                System.out.print(nums[i][j] + " ");
            }

            System.out.println();

        }
    }

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

    public void getIndex(){
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Input Row index: ");
        int rowIndex = scanner.nextInt();
        System.out.println(rowIndex);

        System.out.println("Input Column index: ");
        int colIndex = scanner.nextInt();
        System.out.println(colIndex);

        System.out.println("Output: ");
        System.out.println(nums[rowIndex][colIndex]);
    }

    public void sumRowProducts(){
        int sum = 0;
        System.out.println("Sum of the products of each value in each row: ");

        for(int i = 0; i<nums.length; i++){
            int product = 1;
            for(int j = 0; j<nums[i].length; j++){
                product *= nums[i][j];
            }
            sum += product;
        }
        System.out.println(sum);
    }

    public static void main(String[] args){
        Arrays array = new Arrays();
        array.print();
        array.reverse();
        array.getIndex();
        array.sumRowProducts();
    }
}
Arrays.main(null);
4 16 27 
0 0 12 
3 8 12 


Reverse: 
12
8
3
12
0
0
27
16
4


Input Row index: 
1
Input Column index: 
1
Output: 
0
Sum of the products of each value in each row: 
2016