HuserB1989
HuserB1989

Reputation: 380

Replacing a number in a 2D array in java?

I have a function that finds the first smallest value and returns it from a 2d array:

public static int getMinimumValueOfTerrain(int[][] terrain) {
    int min = Integer.MAX_VALUE;
    for (int row = 0; row < terrain.length; row++) {
        for (int col = 0; col < terrain[row].length; col++) {
            min = Math.min(min, terrain[row][col]);
        }
    }
    return min;
}

I have another function that supposed to replace the found smallest value with its minus value:

public static int[][] replaceMinusValue(int[][] terrain) {
    for (int row = 0; row < terrain.length; row++) {
        for (int col = 0; col < terrain[row].length; col++) {
            int tempValue = getMinimumValueOfTerrain(terrain);
            int minusValue = -tempValue;
        }
    }
    return terrain;
}

Example:

2 1 3
3 2 1
4 2 5

Expected outcome:

2 -1 3
3 2 1
4 2 5

How can I approach it with my functions?

Upvotes: 0

Views: 142

Answers (3)

icolak
icolak

Reputation: 51

You should also keep min’s row and column indices in getMinimumValueOfTerrain. After for loops, you can change terrain[minRow][minColumn] with its negative. I think only getMinimumValueOfTerrain method is enough if there is no constraint. If there is, you can write another function that returns 2-element array of indices.

Upvotes: 2

Shourya Bansal
Shourya Bansal

Reputation: 403

You have multiple options. If you are only doing this once or efficiency does not matter, you can loop through the entire array and find the smallest element, then replace the element. For example:

public static void replaceMinusValue(int[][] terrain) {
    int min = Integer.MAX_VALUE;
    int minI = -1;
    int minJ = -1;
    for (int i = 0; i < terrain.length; i++) {
        for (int j = 0; j < terrain[i].length; j++) {
            if (terrain[i][j] < min) {
                min = terrain[i][j];
                minI = i;
                minJ = j;
            }
        }
    }

    terrain[minI][minJ] *= -1;
}

Would do that. Notice that you don't need to return the terrain after you do the flip (Just like you don't return a new array when sorting).

Upvotes: 2

Nowhere Man
Nowhere Man

Reputation: 19545

Faster implementation would be to detect the row and column coordinates of the minimum value and modify this value:

public static int[][] findAndReplaceMinValue(int[][] terrain) {
    int min = Integer.MAX_VALUE;
    int minRow = -1, minCol = -1;

    for (int row = 0; row < terrain.length; row++) {
        for (int col = 0; col < terrain[row].length; col++) {
            if (terrain[row][col] < min) {
                min = terrain[row][col];
                minRow = row;
                minCole = col;
            }     
        }
    }
    if (minRow != -1 && minCol != -1) {
        terrain[minRow][minCol] *= -1;
    }
    return terrain;
}

If existing method getMinimumValueOfTerrain has to be reused in getMinimumValueOfTerrain, the min element should be re-found once again and break out of the nested loop may be applied:

public static int[][] replaceMinusValue(int[][] terrain) {
    int min = getMinimumValueOfTerrain(terrain);

    out: 
    for (int row = 0; row < terrain.length; row++) {
        for (int col = 0; col < terrain[row].length; col++) {
            if (min == terrain[row][col]) {
                terrain[row][col] *= -1; // or -min
                break out;
            }    
        }
    }
    return terrain;
}

Upvotes: 1

Related Questions