Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static int neighbors(int[][] source, int x, int y) {
        int maxX = Math.min(source[0].length - 1, x + 1);
        int maxY = Math.min(source.length - 1, y + 1);
        int counter = 0;
        for (int i = Math.max(y - 1, 0); i <= maxY; i++) {
            for (int j = Math.max(x - 1, 0); j <= maxX; j++) {

                if (j != x || i != y) {
                    counter += source[i][j];
                }
            }
        }

        return counter;
    }
}