Skip to content

Instantly share code, notes, and snippets.

@kaydell
Last active December 23, 2015 22:29
Show Gist options
  • Save kaydell/6703902 to your computer and use it in GitHub Desktop.
Save kaydell/6703902 to your computer and use it in GitHub Desktop.
Example Java Code for a Matrix which is a two-dimensional array.
/**
* This class demonstrates how to write code that can process a matrix (i.e. a two-dimensional array)
* Note that the method printMatrix will print any two-dimensional array of ints
*
* @author kaydell
*
* Copyright, 2013, Kaydell Leavitt, All Rights Reserved
*
* {link http://simple.wikipedia.org/wiki/Matrix_%28mathematics%29}
*
*/
public class Matrix {
/**
* This method will print any two-dimensional array of ints.
* Note that in Java, a two-dimensional array is implemented
* by an array of arrays.
*
* @param anyIntMatrix
*/
public static void printMatrix(int[][] anyIntMatrix) {
for (int row = 0; row < anyIntMatrix.length; row++) {
for (int column = 0; column < anyIntMatrix[0].length; column++) {
System.out.print(anyIntMatrix[row][column]);
}
System.out.println();
}
}
/**
* This method tests the method called "printMatrix()"
*
* @param args Not used.
*/
public static void main(String[] args) {
int[][] aPaticularIntMatrix = { {1, 2, 3}, { 4, 5, 6} };
printMatrix(aPaticularIntMatrix);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment