patternjavaMinor
Initializing the edge values of 2D array in Java
Viewed 0 times
theinitializingarrayedgejavavalues
Problem
I have been asked to write Java method code to do the below:
I am assuming that they are NOT asking me to create a char[5][5];
i.e :
I am assuming that the method will take a 2D array of int i.e int[][] and just reset the fence values to zero, and not changing the inner values. And that the array sizes is not fixed.
Having assumed that, I came up with below answer:
The response I got was: Isn't there a simpler way to do this?
Below are some of options I later came up with. I would like to know if you know of simpler way to do. Hoping to do this with Java8 features but found it difficult as individual occurrence needed to be identified for location.
```
// Option 2
public int[][] initGrid2(int[][] grid) {
int initVal = 0;
int maxRow = grid.length - 1;
int maxCol = grid[0].length - 1;
for (int rows = 0; rows < grid.length; rows++) {
grid[rows][0] = initVal;
grid[rows][maxCol] = initVal;
for (int cols = 0; cols < grid[0].length; cols++) {
grid[0][cols] = initVal;
grid[maxRow][
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| 0 | | | | 0 |
+---+---+---+---+---+
| 0 | | | | 0 |
+---+---+---+---+---+
| 0 | | | | 0 |
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+I am assuming that they are NOT asking me to create a char[5][5];
i.e :
char[][] grid = {
{'0','0','0','0','0'},
{'0',' ',' ',' ','0'},
{'0',' ',' ',' ','0'},
{'0',' ',' ',' ','0'},
{'0','0','0','0','0'}
};I am assuming that the method will take a 2D array of int i.e int[][] and just reset the fence values to zero, and not changing the inner values. And that the array sizes is not fixed.
Having assumed that, I came up with below answer:
// Original answer
public int[][] initGrid1(int[][] grid) {
int initVal = 0;
int maxRows = grid.length;
int maxCols = grid[0].length;
for (int rows = 0; rows < grid.length; rows++) {
for (int cols = 0; cols < grid[0].length; cols++) {
if (rows == 0 || rows == maxRows - 1) {
grid[rows][cols] = initVal;
}
if (cols == 0 || cols == maxCols - 1) {
grid[rows][cols] = initVal;
}
}
}
return grid;
}The response I got was: Isn't there a simpler way to do this?
Below are some of options I later came up with. I would like to know if you know of simpler way to do. Hoping to do this with Java8 features but found it difficult as individual occurrence needed to be identified for location.
```
// Option 2
public int[][] initGrid2(int[][] grid) {
int initVal = 0;
int maxRow = grid.length - 1;
int maxCol = grid[0].length - 1;
for (int rows = 0; rows < grid.length; rows++) {
grid[rows][0] = initVal;
grid[rows][maxCol] = initVal;
for (int cols = 0; cols < grid[0].length; cols++) {
grid[0][cols] = initVal;
grid[maxRow][
Solution
Could it be that you make too many assumptions? Probably you should really create a new array and not "write the fence" in an existing one.
I guess this would simplify the code by just creating an all zero arrays for the first and last element of the outer array and an array with only the zero at start and the end for all other rows.
Given your assumptions are correct. Using a function to fill the array would simplify your solution significant. In Java this is done with Array.fill.
I guess this would simplify the code by just creating an all zero arrays for the first and last element of the outer array and an array with only the zero at start and the end for all other rows.
Given your assumptions are correct. Using a function to fill the array would simplify your solution significant. In Java this is done with Array.fill.
private static int[][] initGrid(final int[][] grid) {
assert grid.length >= 2;
final int fence = 0;
Arrays.fill(grid[0], fence);
for(int[] row : grid) {
row[0] = row[row.length -1] = fence;
}
Arrays.fill(grid[grid.length - 1], fence);
return grids;
}Code Snippets
private static int[][] initGrid(final int[][] grid) {
assert grid.length >= 2;
final int fence = 0;
Arrays.fill(grid[0], fence);
for(int[] row : grid) {
row[0] = row[row.length -1] = fence;
}
Arrays.fill(grid[grid.length - 1], fence);
return grids;
}Context
StackExchange Code Review Q#147372, answer score: 2
Revisions (0)
No revisions yet.