Java | Dimensional Array Examples

Basic Two Dimensional Array Examples

public class TwoDim {

    public static void main(String [] args) {
        double scores[][] = new double[2][3]; //Number of rows & columns, counting from 1
        //Adding values to each cell
        scores [0][0] = 95;
        scores [0][1] = 88;
        scores [0][2] = 86;
        scores [1][0] = 90;
        scores [1][1] = 78;
        scores [1][2] = 55;

        for(int row=0; row < scores.length; row++) { //Starting from first row, all the way to last row
            for(int col=0; col<scores[row].length; col++) { //Starting from first column, all the way to last column
                System.out.print(scores[row][col] + "\t"); //Print the value in the specified cell
            }
            System.out.println(); //next line
        }
    }
}
public class TwoDimensionalArray {
    public static void main(String [] args) {
        double[][] scores = {{95, 88, 86},{90,78,55}}; //{column1{row 1 elements}column2{row 2 elements}}
        for(int row=0; row < scores.length; row++) { // Start at row 0, to max row, add 1 to row variable to satisfy Loop & to move to next column
            for(int column=0; column < scores[row].length; column++) { //Start at column 0, to max column, add 1 to column variable to satisfy Loop & to move to next column
                System.out.print(scores [row] [column] + " ");
            }
            System.out.println();
        }
    }
}

Results of a two dimensional array in Java

/**
 * 1. (Sum elements column by column)Write a method that returns the sum of all the elemnts in a specified column in a matrix using the following header:
 *     public static double sumColumn(double[][] m, int columnIndex)
 * 2. Write a test program that reads a 3-by-4 matrix and displays the sum of each column.
 *
 * Algorithm:
 * Write a test program that reads a 3 by 4 matrix and displays the sum of each column.
 * > Make a 3 by 4 matrix
 * > Assign values to the 3-by-4 matrix
 * > reads a 3 by 4 matrix
 * > displays the sum of each column
 */

import java.util.Scanner;

public class ColumnSumArray {
    public static void main(String [] args){
        //First things first. Let's make a 3 by 4 matrix.
        double[][] matrix = new double[3][4];

        //Which specific column do you want the sum for?
        Scanner input = new Scanner(System.in);
        System.out.println("Which column Sum do you want? 1, 2, 3, or 4?");
        int columnIndex = input.nextInt();

        double reType = sumColumn(matrix, columnIndex);
        System.out.println();
        System.out.println("The " + columnIndex + " column has a sum of " +reType);

    }

    public static double sumColumn(double[][] matrix, int columnIndex) {

        //Assign values to the matrix
        System.out.println("The Elements in the matrix:");
        for (int row = 0; row < matrix.length; row++) { //start from the 0rth row, don't stop loop until all the rows are done, add 1 to row value each time to satisfy the loop.
            for (int column = 0; column < matrix[row].length; column++) { //start from the 0rth column, ... [same]
                matrix[row][column] = (int) (Math.random() * 100);           //So for the starting from cell of row 0 column 0 all the way to the max row/column cell, fill up the matrix cells with random numbers
            }
        }

        //Print values in matrix
        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[row].length; column++) {
                System.out.print(matrix[row][column] + " ");
            }
            System.out.println();
        }
        System.out.println();
        //Calculate the sum of each matrix column
        //notice that first the column is selected, then the rows. This allows the rows to go through the adding function first, thus "summing elements by column".

        int total =0; //you have to initialize the variable total in order to use it.
        System.out.println("The sum of each column:");
        for (int column = 0; column < matrix[0].length; column++) {
            for (int row = 0; row < matrix.length; row++)
                total += matrix[row][column]; //Notice that braces {} are not used, but the statement above in continued to below and ends at the semi-colon ;
            System.out.print("Sum for column " + column + " is " + total);
            System.out.println();
        }
        return total;
    }
}

Java Dimensional Array sum of all columns code 2

What's Your Opinion?