0

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?

2
  • I didn't look at your code, but from what you're stating that you're trying to accomplish, it looks like you're filling each cell with the value of the column number (outer loop). Why wouldn't you just do something like: for(int i = 0; i < array.length; i++){ for(int j = 0; j < array[0].length; j++){array[i][j] = i;}} Commented Apr 6, 2016 at 22:08
  • What exactly are you trying to accomplish? You never use the grid[][] you pass into the fillDown method. It's unclear what you are trying to do. Commented Apr 6, 2016 at 22:18

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

There is no need of nested loop, just replace each row starting from second with first one.
@svasa This keeps a reference to the original array.
fillDown is not the method with the issue.
1

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)

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.