0

So I need to take a 2D array do calculations to each elements and transfer that into another 2D array while using the values to the "left" "right" "up" and "down" of the current element. If the current element is on the edge (x = 0, y = 0, x = array.length , y = array.length) I will get an array out of bounds error. I want to create a for loop that deals with each of those cases but I don't know how to do it. A sample of my code is

private void buildE(int[][] array, int y, int x)
{

    int up = array[y - 1][x];
    int down = array[y + 1][x];
    int left = array[y][x - 1];
    int right = array[y][x + 1];

    if(up == 0){

        buildETopRow(array);

    }

E will be my new array. This method does not work because y does not equal 0, it just doesn't exist but I can't set ints to null either. In the case of an out of bounds error I need the element (up, down, left, or right) that is out of bounds to equal the current element. Is there a way I can still use a for loop for this or do I need to do something else?

3
  • 1
    What do you want to happen when accessing elements off the edge? Should it wrap around to the other edge, or have a default, or what? Commented Oct 28, 2012 at 22:14
  • Well what I want to do is get the set [i][j] in E to the magnitude of those elements in my original array. So E[i][j] = difference in [i][j] , up, down, left, and right of those all added together. Sorry if that is difficult to follow Commented Oct 28, 2012 at 22:18
  • I edited my original question to clarify Commented Oct 28, 2012 at 22:20

3 Answers 3

1

If I read this correctly you want to effectively treat the difference of an element on the edge with an element off the edge as 0. If that's true I would write four methods right(), left(), up() and down(), with down() shown below as an example:

 /*
  * Return the difference between an element an the element below it
  */

public void down(int x, int y) {

    if (y == array.length - 1) { 
       \\ on the bottom edge
       return 0;
    }   

    return array[y][x] - array[y + 1][x];

}

And inside your loop you'd calculate:

up(x,y) + down(x,y) + right(x,y) + left(x,y)

or whatever calculation it is you need to sum up.

Sign up to request clarification or add additional context in comments.

1 Comment

Well I need to treat an element out of bounds as the current element but this answers my question. Thank you!
0

The easiest way it to surround your array with a border region. So that your x dimension is really width+2.

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int realWidth = 10;
        int realHeight = 10;
        int[][] in = new int[(realWidth+2)][(realHeight+2)];
        int[][] out = new int[(realWidth+2)][(realHeight+2)];
        for (int j = 1;j<realHeight+1;j++)
        {
            for (int i = 1;i<realWidth+1;i++)
            {
                int top = in[j-1][i];
                int bottom = in[j+1][i];
                int left= in[j][i-1];
                int right  = in[j][i+1];
                out[j][i] = operation(top,bottom,left,right);
            }
        }
    }
    public static int operation (int top,int bottom,int left,int right)
    {
        return top+bottom+left+right;
    }
}

Comments

0

I'm not totally sure what your question is, but (1) the usual structure for traversing a 2D array is to use nested for loops (one inside the other), and (2) when you want wrap-around counters (e.g. 2, 3, 0, 1, 2, ...) use the remainder operator %.

int numRows = theArray.length;
int numCols = theArray[0].length;

for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {

        int right = theArray[(j+1) % numCols];
        int down = theArray[(i+1) % numRows];
        int left = theArray[(j+numCols-1) % numCols];
        int up = theArray[(i+numRows-1) % numCols];

        /* right, down, left, and up will be the elements to the right, down, 
           left, and up of the current element. Npw that you have them, you can 
           process them however you like and put them in the other array. */

    }
}

What the remainder operator A%B does is sets A back to zero once it gets as large as B. Since B is the size of your array, that's exactly when it is too large and will cause an IndexOutOfBounds error. Note: That's not how % works but it's an ok way to think of what it does. To find out more about it you can google it, I found an ok explanation here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.