Randy Huang
Randy Huang

Reputation: 113

Java how to fill down a 2d array

So for example, this is what I'm asking: how can you convert this:

{{0, 1, 2},
 {3, 4, 5},
 {6, 7, 8},
 {9, 0, 1}}

to this:

{{0, 1, 2},
 {0, 1, 2},
 {0, 1, 2},
 {0, 1, 2}}

This is what I have so far:

void fillDown(int[][] grid) {
    int[][] m = {{}};
        int[][] newArray = zero(m);
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[0].length; j++) {
                if(j== m.length-1){
                    print(newArray[i][j]);
                }else{
                    print(newArray[i][j] + " ");
                }
            }
        }
    }


    int[][] zero(int[][] m) {
        int rows = m.length;
        int columns = m[0].length;
        int[][] tempArray = new int[rows][columns];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                tempArray[i][j] = m[i][j];
            }
        }
        return tempArray;
}

But, when I input some values, it doesn't work as expected

For example, when I input something like:

{{0, 1, 2},
 {3, 4, 5},
 {6, 7, 8},
 {9, 0, 1}}

it will still return what I inputed:

{{0, 1, 2},
 {3, 4, 5},
 {6, 7, 8},
 {9, 0, 1}}

How would I do this?

Upvotes: 0

Views: 2496

Answers (3)

robotlos
robotlos

Reputation: 536

After staring at your question, it looks like you're talking about your int[][] zero(int[][] m) method (correct me if I'm wrong).

In your int[][] zero(int[][] m) method, replace

    tempArray[i][j] = m[i][j];

with

    tempArray[i][j] = j;

It should look like so:

public static int[][] zero(int[][] m) {
    int rows = m.length;
    int columns = m[0].length;
    int[][] tempArray = new int[rows][columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            tempArray[i][j] = j;
        }
    }
    return tempArray;
}

Running the following:

int[][] array = new int[][]{{0, 1, 2},
                            {3, 4, 5},
                            {6, 7, 8},
                            {9, 0, 1}};
System.out.println("Before:");
for (int[] i : array) {
    for (int j : i) {
        System.out.print(j + " ");
    }
    System.out.println();
}
array = zero(array);
System.out.println("After:");
for (int[] i : array) {
    for (int j : i) {
        System.out.print(j + " ");
    }
    System.out.println();
}

Resulted in:

run:
Before:
0 1 2 
3 4 5 
6 7 8 
9 0 1 
After:
0 1 2 
0 1 2 
0 1 2 
0 1 2 
BUILD SUCCESSFUL (total time: 0 seconds)

Upvotes: 1

SomeDude
SomeDude

Reputation: 14228

Actually just curious why would you want to do this. Anyway, it can be done like :

int[][] a = {{0, 1, 2},
             {3, 4, 5},
             {6, 7, 8},
             {9, 0, 1}};
int[] replacewith = Arrays.copyOf( a[0], a[0].length );
for ( int i = 1; i < a.length; i++ )
{
   a[i] = replacewith;
}

Upvotes: 0

Yassin Hajaj
Yassin Hajaj

Reputation: 21965

Use nested loops.

static void fillDown(int[][] grid) {
    for (int i = 1 ; i < grid.length ; i++){
        for (int j = 0 ; j < grid[i].length ; j++) {
            grid[i][j] = grid[0][j];
        }
    }
}

Or make a copy of the original array.

static void fillDown(int[][] grid) {
    for (int i = 1 ; i < grid.length ; i++){
        grid[i] = Arrays.copyOf(grid[0], grid[0].length);
    }
}

I would not use the following method because it just copies the reference and thus every change made on one of the indexes will impact all the other indexes.

for (int i = 1 ; i < grid.length ; i++) {
    grid[i] = grid[0]; // /!\ NOT A GOOD IDEA
}

Upvotes: 1

Related Questions