1. 배열 선언
데이터형 [][] 배열명;
ex) int [][] num;
2. 배열 생성
배열명 = new 데이터형 [행크기][열크기];
ex) num = new int [][];
3. 배열선언 + 배열생성
데이터형 [][] 배열명 = new 데이터형 [행크기] [열크기];
ex) int [][] num = new int [][];
4. 배열 초기화
배열명[행 인덱스][열 인덱스] = 값;
ex) num[0][0] = 1;
5. 예제) 이차원 배열을 이용한 데이터 처리하기
package ex10_5;
public class Ex10_5 {
public static void main(String[] args) {
int [][] num = new int [3][];
num[0] = new int[3];
num[1] = new int[2];
num[2] = new int[1];
num[0][0] = 1;
num[0][1] = 2;
num[0][2] = 3;
num[1][0] = 4;
num[1][1] = 5;
num[2][0] = 6;
for(int i = 0; i < num.length; i++){
for (int j = 0; j < num[i].length; j++){
System.out.println("num["+i+"]["+j+"] =" + num[i][j]);
}
}
}
}
결과
'프로그래밍 > Java' 카테고리의 다른 글
switch 문 (0) | 2012.06.19 |
---|---|
if 문 (0) | 2012.06.19 |
Runnable 구현하기 (0) | 2012.06.19 |
Thread 구현하기 (0) | 2012.06.19 |
Thread 스레드 (0) | 2012.06.19 |