2차원 배열
: 행렬로 구성된 테이블 행렬
배열 인덱스는 0부터 시작하는거 잊지 말자!
int[][] a = new int[행][열];
int[][] a = new int[2][4];
int[][] a = {{1,2,3,4},{5,6,7,8}} // 초기화 방법
4열 | ||||
2행 | [0][0] | [0][1] | [0][2] | [0][3] |
[1][0] | [1][1] | [1][2] | [1][3] |
public class TwoArray {
public static void main(String[] args) {
int[][] a= {{1,2,3,4},
{5,6,7,8}};
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}

> 결과
//길이가 다른 배열
int[][] b = new int[5][];
b[0] = new int[1];
b[1] = new int[2];
b[2] = new int[3];
b[3] = new int[4];
b[4] = new int[5];
for(int i=0;i<b.length;i++){
for(int j=0;j<b[i].length;j++){
b[i][j]= '*';
System.out.print((char) b[i][j]+"\t");
}
System.out.println();
}
> 결과
'java > 자바_정리' 카테고리의 다른 글
while (0) | 2023.11.20 |
---|---|
for, foreach, 이중for (0) | 2023.11.20 |
배열 (1) (0) | 2023.11.14 |
객체와 클래스 (0) | 2023.11.13 |
개발환경 구축 및 프로그래밍 3대요소.. (11/10) (0) | 2023.11.09 |