---
# System prepended metadata

title: MatrixOperation

---

# MatrixOperation


# LinkedList
## Node
```

```

```java=
package SB3;

  public class MatrixOperation {

          public static int[][] add(int[][] first, int[][] second) {
                  if (first.length != 0 && first.length == second.length) {
                          int result[][] = new int[first.length][first[0].length];
                          for (int i = 0; i < first.length; i++) {
                                  if (first[i].length == second[i].length && first[i].length != 0 && first[i].length == first[0].length)   {
                                          for (int j = 0; j < first[i].length; j++) {
                                                  result[i][j] = first[i][j] + second[i][j];
                                          }
                                  }
                                  else {
                                          return null;
                                  }
                          }
                          return result;
                  }
                  return null;
          }

          public static int[][] multiply(int[][] first, int[][] second) {
                  if (first.length != 0 && second.length != 0) {
                          if (second[0].length > first[0].length || first[0].length == 0 || second[0].length == 0) {
                                  return null;
                          }
                          int result[][] = new int[first[0].length][second[0].length];
                          for (int i = 0; i < result.length; i++) {
                                  if (first[i].length == second.length) {
                                          for (int j = 0; j < second[0].length; j++) {
                                                  for(int k = 0; k < first.length; k++) {
                                                          if (second[k].length == second[0].length) {
                                                                  result[i][j] = result[i][j] + first[i][k] * second[k][j];
                                                          }
                                                          else {
                                                                  return null;
                                                          }
                                                  }
                                          }
                                  }
                                  else {
                                          return null;
                                  }
                          }
                          return result;
                  }
                  return null;
          }

          //public static void main(String[] args){
                  //int[][] empty = {};
                  //int[][] first = {{1,2,3}, {4,5,6}, {7,8,9}};
                  //int[][] second = {{1,2}, {3,4}, {5,6}};

                  //int[][] addResult = add(first, second);
                  //int[][] multiplyResult = multiply(first, second);
                  ¦//addResult is {{6,8},{10,12}}
                  //System.out.println(first[0][0]);
                  //System.out.println(first[0][1]);
                  //System.out.println(addResult[0][0]);
                  //System.out.println(addResult[0][1]);
                  //System.out.println(addResult[0][2]);
                  //System.out.println(addResult[1][0]);
                  //System.out.println(addResult[1][1]);
                  //System.out.println(addResult[1][2]);
                  //System.out.println(multiplyResult[0][0]);
                  //System.out.println(multiplyResult[0][1]);
                  //System.out.println(multiplyResult[1][0]);
                  //System.out.println(multiplyResult[1][1]);
                  //System.out.println(multiplyResult[2][0]);
                  //System.out.println(multiplyResult[2][1]);
                  //System.out.println(empty.length);
          //}
  }
```