system
Given the following post, respond as if you're commenting on Reddit:
human
I want to store values of a 2D array into a 1D array in java.

I want to convert 2D coordinates into a 1D array. I've created an n by n 2D array. I then want to assign values of 1 through N into the 2D array sequentially from left to right, and then return the new 1D array.

    private static int[] to1Dcoords(int row, int column) {
            grid = new int[row][column];                    //creates n by n grid
            int temp = 1;
    
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[i].length; column++) {
                    grid[row][column] = temp;
                    temp++;
                }
            }
            int[] in1Dcoords = new int[grid.length()];
            for (int i = 0; i < in1Dcoords.length; i++) {
                in1Dcoords[i] =
            }
        }

Any help would be much appreciated!

&#x200B;
gpt
You have some syntax errors in your code, but I'll concentrate on your question.You can just loop over your 2D array and assign values to a n*n length 1D array:

int[] array1D = new int[n*n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
array[i+n*j] = grid[i][j];
}
}