Home > Net >  How to code a loop in Java that access a 2D array using the the data stored in another 2D array that
How to code a loop in Java that access a 2D array using the the data stored in another 2D array that

Time:09-17

I have a function that returns all the adyacent positions of a 2D array, something like this:

public static int[][] adyacents(int rows, int columns, int offsetRow, int offsetCol) {...}

The main array and one example of the adyacent array are the following ones to put an example:

char[][] example = {
                {'A', 'B', 'C'},
                {'D', 'E', 'F'},
                {'G', 'H', 'I'}};

int[][] AdyacentsOfA = {
                {0, 1},
                {1, 0},
                {1, 1}};

The only thing I need is to make a loop to access the content of all the different adyacent positions of each position of the main array.

CodePudding user response:

I think I understand the question. Here's one way.

for (int i = 0; i < adyacentsOfA.length; i  ) {
    int row = adyacentsOfA[i][0];
    int column = adyacentsOfA[i][1];
    char c = example[row][column];
    // Process c
 }    
  • Related