[JAVA]동적가변배열

박두팔이·2022년 11월 10일
0

배열을 생성할 때 열에 값을 지정하지 않고 생성한 뒤
열의 length를 행마다 달리 해주는 코드예시

  • int[][] arr01 = new int[5][];
    arr01[0] = new int[1];
    arr01[1] = new int[2];
    arr01[2] = new int[3];
    arr01[3] = new int[4];
    arr01[4] = new int[5];
public class MoveArr {
// 동적 가변 배열
	public static void main(String[] args) {
		int[][] arr01 = new int[5][];
		arr01[0] = new int[1];
		arr01[1] = new int[2];
		arr01[2] = new int[3];
		arr01[3] = new int[4];
		arr01[4] = new int[5];
		int num=1;
		for(int i=0; i<arr01.length; i++) {
			for(int j=0; j<arr01[i].length; j++) {
				arr01[i][j]=num++;
			}
		}
		// 출력하기 
		for(int[] is:arr01){
		System.out.println(Arrays.toString(is));
		}
	}
}
profile
기억을 위한 기록 :>

0개의 댓글