stacy
stacy

Reputation: 15

How do I return such an multi-dimensional array?

The method int[][] labelPath(int n, int[][] points) creates a new square array of length n and returns it back. Each line in data0 describes a point in a two-dimensional array. The column 0 is here always for the column index and column 1 for the row index of a point. If the return array reaches each point in data0 returns the value -1. At all other points, the return array contains the value n.

For example: n = 4 and data0 = {{3, 0}, {0, 1}, {2, 2}} should return:

[[4, 4, 4, -1], [-1, 4, 4, 4], [4, 4, -1, 4], [4, 4, 4, 4]]

My code so far:

int[][] labelPath(int n, int[][] points) {
    int[][] help = new int[n][n];
    for (int i = 0; i < input.length; i++) {
        for (int j = 0; j < input[i].length; j++) {
            int row = input[1].length;
            int column = input[0].length;
            for (int k = 0; k < help.length; k++) {
                for (int l = 0; l < help[k].length; l++) {
                    if (help[i][j] == input[row][column]) {
                        help[i][j] = -1;
                    } else {
                        help[i][j] = n;
                    }
                }
            }
        }
    }
    return help;
}

Upvotes: 1

Views: 266

Answers (3)

user14940971
user14940971

Reputation:

You can use streams to create such an array:

public static int[][] labelPath(int n, int[][] points) {
    // create a new empty 2d array filled with zeros
    int[][] matrix = new int[n][n];
    // set all array elements to 'n'
    Arrays.setAll(matrix, row -> {
        Arrays.fill(matrix[row], n);
        return matrix[row];
    });
    // iterate over the points array and set the corresponding elements to '-1'
    Arrays.stream(points).forEach(row -> matrix[row[1]][row[0]] = -1);
    return matrix;
}
// test
public static void main(String[] args) {
    int n = 4;
    int[][] data0 = {{3, 0}, {0, 1}, {2, 2}};
    int[][] matrix = labelPath(n, data0);

    // output
    Arrays.stream(matrix).map(Arrays::toString).forEach(System.out::println);
}
[4, 4, 4, -1]
[-1, 4, 4, 4]
[4, 4, -1, 4]
[4, 4, 4, 4]

See also: What is the most efficient way to create a 2d string array of initally repetitive data?

Upvotes: 1

WJS
WJS

Reputation: 40044

You can do this very simply as follows:

int[][] nPoints = { { 3, 0 }, { 0, 1 }, { 2, 2 } };
int[][] ret = labelPath(4, nPoints);
for (int[] r : ret) {
    System.out.println(Arrays.toString(r));
}

Prints

[4, 4, 4, -1]
[-1, 4, 4, 4]
[4, 4, -1, 4]
[4, 4, 4, 4]

public static int[][] labelPath(int n, int[][] nPoints) {
    int[][] arr = new int[n][n];
    int[] row = new int[n];
    for (int i = 0; i < n; i++) {
        row[i] = 4;
    }
    // set each row to an array of n elements.
    for (int i = 0; i < n; i++) {
        arr[i] = row.clone(); // new instance each time.
    }
    
    // make the changes
    for (int[] p : nPoints) {
        arr[p[1]][p[0]] = -1;
    }
    return arr;
}

Upvotes: 2

XO56
XO56

Reputation: 330

Create help[n][n] array of 4's.

int[][] help = new int[n][n];
for(int row = 0; row < help.length; row++){
    for(int col = 0; col < 4; col++){
        help[row][col] = 4;
    }
}

Change the value according to data0 array.

int [][]data0 = {{3, 0}, {0, 1}, {2, 2}} ;

for(int row = 0; row < data0.length; row++){
    int ar[] = new int[2];
    for(int col = 0, i = 0; col < data0[row].length; col++, i++){
        ar[i] = data0[row][col];
    }
    help[ar[1]][ar[0]] = -1;
}

Print help[][] array.

for(int row = 0; row < help.length; row++){
    for(int col = 0; col < help.length; col++){
        System.out.print(help[row][col] + " ");
    }
    System.out.println();
}

Output:

4 4 4 -1 
-1 4 4 4 
4 4 -1 4 
4 4 4 4 

Upvotes: 1

Related Questions