So, I have a 4x4 2D array (it will always be these dimensions). Starting with a location on the array, some row and column, I want to find all of its valid neighbors. So far, I
Here's how I'd do it: a method that gets a list of x,y pairs for valid neighbors, given an arbitrary [x,y]
point and generalized to any array size:
public List<int[]> getNeighbors(x, y, maxX, maxY) {
neighbors = new ArrayList<int[]>;
if x > 0:
neighbors.add({x-1, y});
if y > 0:
neighbors.add({x, y-1});
if x < maxX:
neighbors.add({x+1, y});
if x < maxY:
neighbors.add({x, y+1});
return neighbors;
}
[...]
for (int[] coords : getNeighbors(x, y, 4, 4)) {
// do stuff
}
public class FindingNeighboursInMatrix {
public static void main(String[] args) {
int array[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
System.out.println("neightbours of " + array[i][j]);
int neb[] = findneighbours(i, j, array);
for (int k = 0; k < neb.length; k++) {
if (neb[k] != -1) {
System.out.print(" " + neb[k] + ",");
}
}
System.out.println();
}
}
}
public static int[] findneighbours(int i, int j, int matrix[][]) {
int neb[] = new int[8];
// top row
neb[0] = getvalue(i - 1, j - 1, matrix);
neb[1] = getvalue(i - 1, j, matrix);
neb[2] = getvalue(i - 1, j + 1, matrix);
// left element
neb[3] = getvalue(i, j - 1, matrix);
// right element
neb[4] = getvalue(i, j + 1, matrix);
// bottom row
neb[5] = getvalue(i + 1, j - 1, matrix);
neb[6] = getvalue(i + 1, j, matrix);
neb[7] = getvalue(i + 1, j + 1, matrix);
return neb;
}
public static int getvalue(int i, int j, int matrix[][]) {
int rowSize = matrix.length;
int colSize = matrix[0].length;
if (i < 0 || j < 0 || i > rowSize - 1 || j > colSize - 1) {
return -1;
}
return matrix[i][j];
}}
This is my solution:
public int[4][4] array2d;
//don't forget to fill it!
private void adjustNeighbors(int xCoord, int yCoord) {
for (int yi = y-1; yi <= yCoord+1; yi++) { //loop through the neighbors
for (int xi = x-1; xi <= xCoord+1; xi++) {
try {
if (!(xCoord != xi && yCoord != yi)) {
array2d[y][x]++; //do whatever you want to all the neighbors!
}
} catch (Exception e) {
// something is out of bounds
}
}
}
}
Unfortunately by writing code you are telling a computer what to do and the computer doesn't know anything more than what you tell it.
You can automate this kind of thing a little with nonstandard loop logic though I guess:
for (int coff = -1; coff < 3; coff += 2) {
for (int roff = -1; roff < 3; roff += 2) {
if ( col + coff >= 0 &&
col + coff < array.length &&
row + roff >= 0 &&
row + roff < array[row].length) {
// do stuff with array[col + coff][row + roff]
}
}
}
That loop structure will flip the column and row offset from -1 to 1 and then break when they become 3 on the 3rd iteration.
But note that in your code, checking against !(stuff) > 4 will give you an ArrayIndexOutOfBounds exception because remember the last index is 4 - 1.
For any 2D array cellValues[][]
of (x,y)
dimensions below code can be used for getting all 8 neighbors for any cell (i,j)
. Code will return 0
by default.
public static ArrayList<Integer> getNeighbors(int i, int j, int x, int y, int[][] cellValues) {
ArrayList<Integer> neighbors = new ArrayList<>();
if(isCabin(i, j, x, y)) {
if(isCabin(i + 1, j, x, y))
neighbors.add(cellValues[i+1][j]);
if(isCabin(i - 1, j, x, y))
neighbors.add(cellValues[i-1][j]);
if(isCabin(i, j + 1, x, y))
neighbors.add(cellValues[i][j+1]);
if(isCabin(i, j - 1, x, y))
neighbors.add(cellValues[i][j-1]);
if(isCabin(i - 1, j + 1, x, y))
neighbors.add(cellValues[i-1][j+1]);
if(isCabin(i + 1, j - 1, x, y))
neighbors.add(cellValues[i+1][j-1]);
if(isCabin(i + 1, j + 1, x, y))
neighbors.add(cellValues[i+1][j+1]);
if(isCabin(i - 1, j - 1, x, y))
neighbors.add(cellValues[i-1][j-1]);
}
return neighbors;
}
public static boolean isCabin(int i, int j, int x, int y) {
boolean flag = false;
if (i >= 0 && i <= x && j >= 0 && j <= y) {
flag = true;
}
return flag;
}
The way I would do it would be to have a seperate method.
public void example(int changeSign, boolean shouldCheckRow,boolean shouldCheckColumn){
int num = 4;
if(changeSign < 0)
num = 0;
if(shouldCheckRow)
//adding a negative is the same as subtracting so if you add -1, you're really subtracting by one.
if(!((row + changeSign) < num))
//do stuff
else
if(!((col + changeSign) < num))
//do stuff
}
And the method calls would be
public static void main(String args[]){
int shouldTestRight = 1;
int shouldTestLeft = -1;
int shouldTestUp = 1;
int shouldTestDown = -1;
// so if you want to test up or right, the first parameter should be positive
// if you want to test for down or left, the first parameter should be negative
// this is because the negative will flip the sign.
// if you should change the row, the second parameter should be true
// if you should change the column, the third parameter should be true.
example(shouldTestRight,true,false);
example(shouldTestLeft,true,false);
example(shouldTestUp,false,true);
example(shouldTestDown,false,true);
}
Of course, you don't have to include the extra ints in the method you're calling from but I did it for extra code readability.