1

I need a method that given input 2D array {{1,2},{3,4}} and (int)row=2; (int)column = 3, will produce a concatenated 2D array {{1,2,1,2,1,2}{3,4,3,4,3,4}}.

My attempt was to use a nested for loop to expand them both horizontally and and vertically, but was unsuccessful. This is what I have so far:

    int row = 2;
    int column = 5;
    int count = 0;
    int[][] list = {{12,3},{3,4}};

    int [][] renewed = new int[row*list.length][column*list[0].length]; 

    for (int l = 0; l<list.length; l++) {
        for (int k = 0; k<renewed.length; k+= list.length) {
            renewed[l+k] = list[l];
        }
    }        

    System.out.println(Arrays.deepToString(renewed));       
    }
}

^This produces list[][] expanded vertically, for the first column

    int row = 2;
    int column = 4;
    int[][] list = {{12,3},{3,4}};

    int [][] renewed = new int[row*list.length][column*list[0].length]; 
    for (int i = 0; i<list[0].length; i++) {
        for (int j = 0; j<renewed[0].length; j+=list[0].length) {
            renewed[0][j+i] = list[0][i];
        }
    }

    System.out.println(Arrays.toString(renewed[0]));       
}

^This produces list[][] expanded horizontally, for the first row;

So how can I concatenate these two methods in order to produce a method that expands BOTH horizontally and vertically?

6
  • How would input 3 produce an array of size 5 ? Commented Nov 14, 2015 at 21:44
  • I am just testing out with different values. You can ignore specific values that I used, as I am looking for a general answer, not one tailored to this specific array. Commented Nov 14, 2015 at 21:46
  • but this does not help in solving the problem actually. How do you want your original array being modified exactly? Commented Nov 14, 2015 at 21:47
  • 1
    I am converting a jpg image to a 2D array, then making a "tile" method to create multiples of that array. So that I can convert it back to image and get multiple images of the same picture. Commented Nov 14, 2015 at 21:50
  • 1
    If user inputs 3 for row and 5 for column, my image would be 3x5 of that image with 15 individual images glued together. Commented Nov 14, 2015 at 21:52

1 Answer 1

2

I think the easiest way is to iterate over every position in the new array and use the remainder operator % to get the right entry of the original.

int[][] list = {{1,2},{3,4}};
int row = 2;
int column = 5;
int [][] renewed = new int[row*list.length][column*list[0].length];
for (int i = 0; i < renewed.length; i++) {
    for (int j = 0; j < renewed[0].length; j++) {
        renewed[i][j] = list[i % list.length][j % list[0].length];
    }
}
System.out.println(Arrays.deepToString(renewed));
Sign up to request clarification or add additional context in comments.

1 Comment

@mathlover No problem. Glad I could help.

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.