[Java] 배열 확장

프로그래밍/JAVA 2018. 4. 3. 21:17 posted by 야매코더

배열 확장

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
public class EnlargeMatrix {
 
    public static int[][] getEnlargeMatrix(int[][] src, int enlargeNum) {
        int nWidth = src.length;
        int nHeight = src[0].length;
        int[][] tmpMtx = new int[nWidth * enlargeNum][nHeight * enlargeNum];
 
        for (int i = 0; i < nWidth; i++) {
            for (int j = 0; j < nHeight; j++) {
                tmpMtx[i * enlargeNum][j * enlargeNum] = src[i][j];
                tmpMtx[i * enlargeNum + 1][j * enlargeNum] = src[i][j];
                tmpMtx[i * enlargeNum][j * enlargeNum + 1] = src[i][j];
                tmpMtx[i * enlargeNum + 1][j * enlargeNum + 1] = src[i][j];
            }
        }
        return tmpMtx;
    }
 
    public static void main(String[] args) {
        int[][] table = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
        int[][] result = getEnlargeMatrix(table, 2);
        for (int j = 0; j < result.length; j++) {
            for (int i = 0; i < result[0].length; i++) {
                System.out.print(result[i][j] + "   ");
            }
            System.out.println();
        }
    }
 
}