프로그래밍/Java

    다차원 배열의 clone

    배열의 복사 배열을 복사할 때는 clone을 이용한다 int[] a = {1,2,3}; int[] b = a.clone(); 복사된 배열을 출력해보면 배열의 요소들이 잘 복사된 것을 확인할 수 있다 System.out.println("a 배열"); for (int i : a) { System.out.print(i); } System.out.println("\n"); System.out.println("b 배열"); for (int i : b) { System.out.print(i); } 출력결과 a 배열 123 b 배열 123 서로 참조하는 대상이 다른 것도 확인할 수 있다 System.out.println("a 배열의 주소"); System.out.print(a); System.out.println("..

    printf + String.format (%%%을 보고 당황했다면..)

    %%% 다음과 같이 해괴망측한 코드가 있다 %가 무려 세번이나 쓰였다 int n = 2; int r = 1; int x = 3; System.out.printf(String.format("%%2d | %%%dd\n", n), r, x); 다음과 같은 이상한 코드는 format에 의해 1. %% → %으로 바뀌고 System.out.printf(String.format("%2d | %%dd\n", n), r, x); 2. %d자리에 n이 들어가게 된다 System.out.printf("%2d | %2d\n", r, x); 최종적으로 다음이 출력된다 (%2d는 2자리 보다 작으면 공백을 추가하여 출력이 됨을 의미) 1 | 3 %%%을 사용하면 공백을 변수로 조절할 수 있게된다