I have a 2D array that gets dynamically created by any integer N. Initially, I need to set all values in the array to a value that represents it being "uninitialized" where I use the number "-1". However, I want to convert this 2D array to a 1D array and assign every value to be equal to its index in the new 1D array.
public class Percolation {
private int[][] id;
private int[] array1D;
private int blocked = -1; //a number that doesn't exist in the array
// create N-by-N grid, with all sites blocked
public Percolation(int N){
id = new int[N][N];
for (int k = 0; k < N; k++)
{ for (int i = 0; i < N; i++) id[k][i] = blocked; }
}
// open site (row i, column j) if it is not already
public void open(int i, int j){
}
In the open method it should change the value at the given indexes to be the corresponding index in a 1D array. For example:
[-1] [-1]
[-1] [-1]
Would then become:
[0] [1] [2] [3]
Unfortunately, since this is homework I'm unsure how I can share the grid size "N" to be able to create a new 1D array with the indexes as the values.
Nin your class withid.length.