Java examples for Data Structure:Matrix
Add two Matrix together
import java.util.Scanner; public class Main { public static int[][] mMatrixSum(int mArrayA[][], int mArrayB[][]) { int i, j;/*from w ww.j av a 2 s. c o m*/ int mRows = mArrayA.length; int mColumn = mArrayA[0].length; int[][] mSumArray = new int[mRows][mColumn]; for (i = 0; i < mRows; i++) { for (j = 0; j < mColumn; j++) { mSumArray[i][j] = mArrayA[i][j] + mArrayB[i][j]; } } return mSumArray; } public static void main(String args[]) throws Exception { int r, c; int i, j; int mRow = 4; int mColumn = 3; int[][] mArrayA = new int[mRow][mColumn]; for (r = 0; r < mArrayA.length; r++) { for (c = 0; c < mArrayA[r].length; c++) { mArrayA[r][c] = c + r; System.out.print(mArrayA[r][c] + " "); } System.out.println(" "); } int mRowTwo = 4; int mColumnTwo = 3; int[][] mArrayB = new int[mRowTwo][mColumnTwo]; for (r = 0; r < mArrayB.length; r++) { for (c = 0; c < mArrayB[r].length; c++) { mArrayB[r][c] = r*c; System.out.print(mArrayB[r][c] + " "); } System.out.println(" "); } int sum[][] = mMatrixSum(mArrayA, mArrayB); System.out.println("Sum Of Matirx"); for (i = 0; i < sum.length; i++) { for (j = 0; j < sum[i].length; j++) { System.out.print(" " + sum[i][j]); } System.out.println(); } } }