# Multiply two matrices : Array « Collections Data Structure « Java

Multiply two matrices

```
/**
* Multiply two matrices.
* Only defined for int: TODO: rewrite using 1.5 Generics to add
* support for long, float, and double.
* @author Ian F. Darwin, http://www.darwinsys.com/
* @version \$Id: Matrix.java,v 1.4 2004/03/07 02:53:53 ian Exp \$
*/
public class Matrix {

/* Matrix-multiply two arrays together.
* The arrays MUST be rectangular.
* @author Tom Christiansen & Nathan Torkington, Perl Cookbook version.
*/
public static int[][] multiply(int[][] m1, int[][] m2) {
int m1rows = m1.length;
int m1cols = m1[0].length;
int m2rows = m2.length;
int m2cols = m2[0].length;
if (m1cols != m2rows)
throw new IllegalArgumentException("matrices don't match: " + m1cols + " != " + m2rows);
int[][] result = new int[m1rows][m2cols];

// multiply
for (int i=0; i<m1rows; i++)
for (int j=0; j<m2cols; j++)
for (int k=0; k<m1cols; k++)
result[i][j] += m1[i][k] * m2[k][j];

return result;
}

/** Matrix print.
*/
public static void mprint(int[][] a) {
int rows = a.length;
int cols = a[0].length;
System.out.println("array["+rows+"]["+cols+"] = {");
for (int i=0; i<rows; i++) {
System.out.print("{");
for (int j=0; j<cols; j++)
System.out.print(" " + a[i][j] + ",");
System.out.println("},");
}
System.out.println(":;");
}
public static void main(String[] argv) {
//+
int x[][] = {
{ 3, 2, 3 },
{ 5, 9, 8 },
};
int y[][] = {
{ 4, 7 },
{ 9, 3 },
{ 8, 1 },
};
int z[][] = Matrix.multiply(x, y);
Matrix.mprint(x);
Matrix.mprint(y);
Matrix.mprint(z);
//-
}
}

```

### Related examples in the same category

 1 Initialize a static array 2 Initialization and re-assignment of arrays 3 Doubling the size of an array 4 Timing array loop performance 5 Array 2D 6 Can you change the .length of an array 7 Show Two-Dimensional Array of Objects 8 ArrayListDemo done over using an ArrayList 9 Array Hunt game 10 Multi Dimension Array 11 Clone Array 12 Associates keys with values 13 Arrays of primitives 14 Creating arrays with new 15 Array initialization 16 Creating an array of nonprimitive objects 17 Create multidimension arrays 18 Initializing Array Values 19 Creating a Two-Dimensional Array 20 Initializing a Two Dimensional Array 21 Using the length Variable 22 Triangular array 23 Grow array 24 Define array for class 25 String array and output to console 26 Array Of Arrays Demo 2 27 Array Copy Demo 28 Copying Elements from One Array to Another 29 Java program to demonstrate multidimensional arrays 30 Extend the size of an array 31 Copy an array 32 Initialize multidimensional array 33 Get array upperbound 34 To get the number of dimensions 35 Resize an array, System.arraycopy() 36 Dump array content: Convert the array to a List and then convert to String 37 java.utils.Arrays provides ways to dump the content of an array. 38 Dump multi-dimensional arrays 39 Use the new shorthand notation to iterate through an array 40 Create a repeated sequence of character 41 Reverse array elements order 42 Convert array of primitives into array of objects 43 Array Initializers 44 Reinitializes a byte array 45 Reinitializes an int array 46 Sum all elements in the array 47 Sums an array of numbers log(x1)...log(xn) 48 A class to iterate over all permutations of an array. 49 Palidrome Array 50 Set of utilities used to manipulate arrays. 51 ArrayUtils provides static methods for manipulating arrays when using a tool such as java.util.ArrayList is inconvenient. 52 Array Util 53 clone two dimensional array