[Java] 다차원 배열 , 2차원 배열 사용해보기
by Roel Downey728x90
반응형
다차원 배열이란?
2차원 이상의 배열을 의미하며, 배열 요소로 또 다른 배열을 가지는 배열을 의미
2차원 배열은 배열 요소로 1차원 배열을 가지는 배열이며,
3차원 배열은 배열 요소로 2차원 배열을 가지는 배열이고,
4차원 배열은 배열 요소로 3차원 배열을 가지는 배열이다.
2차원 배열
선언 방법
자료형[ ][ ] 배열이름 = new 자료형[ ][ ]; |
int[][] array = new int[2][3];
array [0][0] | array [0][1] | array [0][2] |
array [1][0] | array [1][1] | array [1][2] |
초기화 방법
초기화 하는 방법은 여러가지가 있다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayStudy { | |
public static void main(String[] args) { | |
int[][] array = new int[2][3]; | |
int value = 10; | |
for (int i = 0; i < array.length; i++) { | |
for (int j = 0; j < array[i].length; j++) { | |
array[i][j] = value; | |
value += 10; | |
} | |
} | |
for (int i = 0; i < array.length; i++) { | |
for (int j = 0; j < array[i].length; j++) { | |
System.out.print(array[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayStudy01 { | |
public static void main(String[] args) { | |
int[][] array = new int[2][3]; | |
int value = 10; | |
array[0][0] = value; | |
array[0][1] = value+10; | |
array[0][2] = value+20; | |
array[1][0] = value+30; | |
array[1][1] = value+40; | |
array[1][2] = value+50; | |
} | |
} |
728x90
반응형
블로그의 정보
What doing?
Roel Downey활동하기
What doing?Roel Downey 님의 블로그입니다.