I have an addition to a question I asked earlier. I have a 2D array that I need to get the magnitude of each element based on the 4 surrounding elements. Surrounding being up, down, left and right. If one or more of the surrounding elements goes out of the array bounds, i.e. the current element is on an edge, it treats the out of bounds element as the current. My program only works when the array is a square, 4 x 4, 5 x 5, ect. but when it is a rectagle, 4 x 5, 5 x 6, ect I get an error. I believe this is due to the fact that array.length is no longer the same for x and y. I do not know how to correct this error, any help would be appreciated! This is my current code:
public class ArrayTest
{
public static int[][] buildE(int[][] array)
{
int [][] arrayE = new int[array.length][array.length];
int up;
int down;
int left;
int right;
for (int y = 0; y < array.length; y++)
{
for (int x = 0; x < array[y].length; x++)
{
//if element is on the top left
if (y == 0 && x == 0)
{
up = array[y][x];
down = array[y + 1][x];
left = array[y][x];
right = array[y][x + 1];
}
//if element is on bottom right
else if (y == array.length - 1 && x == array.length - 1)
{
up = array[y - 1][x];
down = array[y][x];
left = array[y][x - 1];
right = array[y][x];
}
//if element is on top right
else if(y == 0 && x == array.length - 1)
{
up = array[y][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x];
}
//if element is on bottom left
else if (y == array.length - 1 && x == 0)
{
up = array[y - 1][x];
down = array[y][x];
left = array[y][x];
right = array[y][x + 1];
}
//if element is on top
else if (y == 0)
{
up = array[y][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x + 1];
}
//if element is on left
else if (x == 0)
{
up = array[y - 1][x];
down = array[y + 1][x];
left = array[y][x];
right = array[y][x + 1];
}
//if element is on bottom
else if(y == array.length - 1)
{
up = array[y - 1][x];
down = array[y][x];
left = array[y][x - 1];
right = array[y][x + 1];
}
//if element is on right
else if (x == array.length - 1)
{
up = array[y - 1][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x];
}
//if element is not on an edge
else
{
up = array[y - 1][x];
down = array[y + 1][x];
left = array[y][x - 1];
right = array[y][x + 1];
}
int element = array[y][x];
int magnitude = Math.abs(element - up) + Math.abs(element - down) + Math.abs(element - left) + Math.abs(element - right);
System.out.println();
System.out.print("#####################################");
System.out.println();
System.out.println("Array Element: " + array[y][x]);
System.out.println("Up: " + up);
System.out.println("Down: " + down);
System.out.println("Left: " + left);
System.out.println("Right: " + right);
System.out.println("Magnitude: " + magnitude);
System.out.println("X: " + x);
System.out.println("Y: " + y);
System.out.println("Array Length: " + array.length);
arrayE[y][x] = magnitude;
}
}
return arrayE;
}
public static void outputArray(int[][] array)
{
for(int row = 0; row < array.length; row ++)
{
for (int column = 0; column < array[row].length; column++)
System.out.printf("%d ", array[row][column]);
System.out.println();
}
}
public static void main(String[] args)
{
int [][] myArray = {{1, 12, 13, 14, 15}, {2, 22, 23, 24, 25}, {3, 32, 33, 34, 35}, {4, 42, 43, 44, 45}, {5, 52, 53, 54, 55}, {6, 62, 63, 64, 65}};
outputArray(myArray);
outputArray(buildE(myArray));
}
}