Contents
2 차원 배열2 차원 배열
- 배열과 같이 많은 데이터를 하나로 묶어서 처리하지만 ‘1 차원’이 아니라 ‘2 차원’으로 저장을 한다.
- 배열과 같이 공간을 늘릴 수 없지만 값의 변경은 가능하다. ( 구조 변경이 불가능 하다. )
래그드 배열
- 2 차원 배열에서 행마다 길이가 다른 배열을 말한다.
기본 구조
int[][] s = new int[3][5]; // 앞은 행 뒤는 열
누적 코드 : 자기 자신에 자신의 값을 넣는 것
리펙토리 : 코드를 깔끔하게 정리 하는 것
2 차원 배열 예제
package ex03; // 2차원 배열 예제 public class TheaterSeats { public static void main(String[] args) { // 배열은 구조 변경 불가능 int[][] seats = { {0, 0, 0, 1, 1, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 1, 1, 0} }; int sum = 0; int count = 0; //int row = -1; for (int row = 0; row < seats.length; row++) { count = 0; for (int i = 0; i < seats[row].length; i++) { count = count + seats[row][i]; } System.out.println(row + "번째 행의 관객수는: " + count); sum = sum + count; } System.out.println("전체 관계수는: " + sum); } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252F6c6b9177-49c1-45c9-b111-4f5ff9a6b861%252FUntitled.png%3Ftable%3Dblock%26id%3Dd85bf94f-e6c7-43eb-bce6-55df09051d5c%26cache%3Dv2&w=3840&q=75)
모듈화 및 단계 과정이 존재 하는 이유는
코딩 할 때 반복 해야 하는 상황을 대비하여 모듈화(일관성 있게 코드를 변경)를 진행하며
단계 과정은 순차적으로 코드를 파악해야 쉽게 모듈화를 가능하기에 진행한다.
2 차원 배열 예제 모듈화 과정
// 모듈화 및 단계 과정 - 앞의 코드에 대입해서 사용 가능하다. row++; count = 0; for (int i = 0; i < seats[row].length; i++) { count = count + seats[row][i]; } System.out.println(row + "번째 행의 관객수는: " + count); sum = sum + count; row++; count = 0; for (int i = 0; i < seats[row].length; i++) { count = count + seats[row][i]; } System.out.println(row + "번째 행의 관객수는: " + count); sum = sum + count; row++; count = 0; for (int i = 0; i < seats[row].length; i++) { count = count + seats[row][i]; } System.out.println(row + "번째 행의 관객수는: " + count); sum = sum + count; row++; count = 0; for (int i = 0; i < seats[row].length; i++) { count = count + seats[row][i]; } System.out.println(row + "번째 행의 관객수는: " + count); sum = sum + count; System.out.println("전체 관계수는: " + sum);
래그드 배열 1번 예제
package ex03; /** * 레그드 배열 * 2차원 배열에서 행마다 길이가 다른 배열을 말한다. */ public class RaggedArray { public static void main(String[] args) { int[][] ragged = new int[3][]; // 행이될 배열을 선언 ragged[0] = new int[1]; // 행마다 배열의 크기를 지정 ragged[1] = new int[2]; ragged[2] = new int[3]; for (int i = 0; i < ragged.length; i++) { for (int j = 0; j < ragged[i].length; j++) { ragged[i][j] = j; System.out.print(ragged[i][j] + " "); } System.out.println(); } } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252Fc999b4ab-7c37-4b30-b884-bbb6d7ac064c%252FUntitled.png%3Ftable%3Dblock%26id%3Ded660111-c42f-4c43-98b3-6ec5b11c33ea%26cache%3Dv2&w=3840&q=75)
래그드 배열 2번 예제
package ex03; import java.util.Arrays; public class RaggedArray2 { public static void main(String[] args) { int[][] rarray = new int[3][]; rarray[0] = new int[]{1, 2, 3, 4}; rarray[1] = new int[]{5, 6, 7}; rarray[2] = new int[]{8, 9}; for (int i = 0; i < rarray.length; i++) { // 전통 방식으로 값을 출력 for (int j = 0; j < rarray[i].length; j++) { System.out.print(rarray[i][j] + " "); } System.out.println(); } for (int[] row : rarray) { // 1 차원 배열이면 이러한 방식으로 가능하지만 2차원 배열은 불가능하다 System.out.println(Arrays.toString(row)); } } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252Fcbf8aee0-2270-49c7-b2fa-479193ef7da1%252FUntitled.png%3Ftable%3Dblock%26id%3D6ecb88e8-673f-4338-bbe9-60c75b66d620%26cache%3Dv2&w=3840&q=75)
Share article