title: Java - Get all values around a number
body: <p>I want to make a Matrix which changes the values/make a sum of the values of the number on the place plus over, under, left and right of it.</p>
  
  <ul>
  <li>When there is no element then it should be added 0.</li>
  </ul>
  
  <p>Currently the problem is that <code>9  3  9 | 6  12  6 | 9  3  9</code> but they should be <code>9 10 9 | 6 10 6| 9 10 9</code> (just from left to right and left to right) and I don't know who I get the values above.</p>
  
  <p>Can anybody help me with that? </p>
  
  <p>Furthermore I wanted to do that in a loop that is automatic but then I get always get this <code>ArrayIndexOutOfBoundsException</code>. </p>
  
  <ul>
  <li><p>The Input Matrix is <code>1 8 1 | 4 2 4 | 1 8 1</code> and the outcome should be <code>13 12 13 | 8 26 8 | 13 12 13</code></p>
  
  <pre><code>public class Matrix {
      public static void main(String[] args) {
  
      int[][] matrix = { { 1, 8, 1 }, { 4, 2, 4 }, { 1, 8, 1 } };
      print(matrix);
  
      System.out.println("\n");
      int[][] blur = blurMatrix(matrix);
      print(blur);
  }
  
  public static int[][] blurMatrix(int[][] matrix) {
      if (matrix == null)
          return null;
      if (matrix.length &lt;= 0)
          return null;
  
      int[][] blur = new int[matrix.length][];
  
      for (int row = 0; row &lt; blur.length; row++) {
          blur[row] = new int[matrix[row].length];
  
          for (int col = 0; col &lt; blur[row].length - 1; col++) {
              int cellValue = matrix[row][col];
              int nextColValue = matrix[row][col + 1];
              // int lastColValue = matrix[row][col-1];
              // int nextRowValue = matrix[row+1][col];
              // int lastRowValue = matrix[row-1][col];
  
              blur[row][col] = cellValue + nextColValue;// +lastColValue+nextRowValue+lastRowValue;
          }
  
          int lastColumnIndex = blur[row].length - 1;
          blur[row][2] = blur[row][0];
          blur[row][1] = matrix[row][2] + matrix[row][0] + matrix[row][2];
  
      }
  
      return blur;
  
  }
  
  public static void print(int[][] m) {
  
      if (m == null) {
          System.out.print(" no matrix to print!");
          return;
      }
  
      for (int row = 0; row &lt; m.length; row++) {
  
          for (int col = 0; col &lt; m[row].length; col++) {
  
              System.out.print(m[row][col] + "  ");
  
          }
          System.out.println("");
      }
  
  }
  }
  </code></pre></li>
  </ul>