Implementing A Matrix in Java

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.

Background and Explanation of the code


In this program we are using a two dimensional array to demonstrate matrix using Java. We declare a new class called Matrix which has a static method outputArray(). This method takes an integer array as parameter.  The methodarray.length() is used to find the length of the row and column.

In the method outputArray(), we get the length of the row and the column. We then iterate through the array with the help of nested for loop. The outer loop iterates over the row, while the inner loop iterates over the inner loops.

Output

run:

Row size= 2

Column size = 9

[ 1 3 5 7 8 9 10 11 14 ]

[ 2 4 6 2 1 3 8 9 2 ]