Island perimeter code challenge in Java Problem:


  Source (with example): https://leetcode.com/problems/island-perimeter/
  
  You are given a map in form of a two-dimensional integer grid where 1
  represents land and 0 represents water.
  
  Grid cells are connected horizontally/vertically (not diagonally). The
  grid is completely surrounded by water, and there is exactly one
  island (i.e., one or more connected land cells).
  
  The island doesn't have "lakes" (water inside that isn't connected to
  the water around the island). One cell is a square with side length 1.
  The grid is rectangular, width and height don't exceed 100. Determine
  the perimeter of the island.


Below is my solution. I haven't coded in Java since college and I'm trying to pick it up again. How is my style and is there any new Java features I can apply to the method to make it more concise? Thank you in advance!

Code:

class Solution {
    private int m;
    private int n;

    public int islandPerimeter(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int perimeter = 0;

        for (int i = 0; i &lt; m; i++) {
            for (int j = 0; j &lt; n; j++) {
                if (grid[i][j] == 1) {
                    perimeter += getPerimeter(i, j, grid);
                }
            }
        }
        return perimeter;
    }

    private int getPerimeter(int i, int j, int[][] grid) {
        int perimeter = 0;
        if (i == 0 || grid[i-1][j] == 0) {
            perimeter++;
        }
        if (j == 0 || grid[i][j-1] == 0) {
            perimeter++;
        }
        if (i == m-1 || grid[i+1][j] == 0) {
            perimeter++;
        }
        if (j == n-1 || grid[i][j+1] == 0) {
            perimeter++;
        }
        return perimeter;
    }
}
Instead of looking for cool new features of Java i would strongly suggest that you concentrate on the basics of Java: Objects:

Think in Objects:

So far you have identified these Objects: Map, a Cell and an Island - so make some Objects of that type.

Map map = new Map(int[][] src);
Island island = map.extractIsland();
int diameter = island.getPerimater();


the Map needs some methods to properly extract the island

Cell cell = getCellAt(int x, int y);


and a method to gain relationship between each Cells on the map:

List&lt;Cell&gt; getNeighbours(Cell center);


and some elementary methods on the Cell class

boolean isWater();
boolean isLand();
int getAmountCoasts();


if you would have these elementary Objects you can simply create readable Code - see this example:

Cell center; //yes, here center.isLand() = true
List&lt;Cell&gt; neigbour = map.getNeighbours(center);
for (Cell neigbour: neigbours){
    if (neigbour.isWater()){
        center.addCoast();
    }
}


once you are here you can use features from java 8

List&lt;Cell&gt; landCells = cells.stream.filter(Cell::isLand).collect(CollectorsToList());
int perimeter = landCells.stream.mapToInt(Cell::.getAmountCoasts).sum();


Hint: you better let the Island class handle that, see first hints - Responsibility for coastLine is within the Island class!)

NOTE:

it's hard to think in objects whenever you try minimal code snippets as suggested from leet code. if minimal code is required, look maybe you are interested at CodeGolf...