자료형이 같은 1차원 배열을 묶음으로 다룸(행, 열)
자료형[ ][ ] 배열변수명 = new 자료형[행][열]
초기화 방법
① 직접 초기화
ex) arr=[0][0]
② for문 이용
for(int row = 0 ; row < arr.length ; row++) {
for(int col = 0 ; col < arr[row].length ; col++) {
arr[row][col] = num;
num += 10;
}
char[][] arr = new char[4][]; // 행 부분만 생성
arr[0] = new char[3]; // 0행에 3열짜리 1차원 배열 생성
arr[1] = new char[4];
arr[2] = new char[5];
arr[3] = new char[2];
char ch = 'a';
for(int i = 0 ; i < arr.length ; i++) {
for(int j = 0 ; j < arr[i].length ; j++) {
arr[i][j] = ch++
}
public class Array2Practice {
Scanner sc = new Scanner(System.in);
public void prac1() {
int[] bingo = new int[25];
int index = 0;
for(int x = 0 ; x < 25 ; x++) {
int random = (int)(Math.random()*25+1);
bingo[x] = random;
for(int y = 0 ; y < x ; y++) {
if(bingo[y] == random) {
x--;
break;
}
}
}
--------------------------------// 1차원 배열을 이용하여 25까지 난수 생성(중복 제거)
int[][] arr = new int[5][5];
for(int i = 0 ; i < 5 ; i++) {
for(int j = 0 ; j < 5 ; j++) {
arr[i][j] = bingo[index++];
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.print("1~25의 숫자를 입력하세요(종료 : 0) = ");
int input = sc.nextInt();
--------------------------------// 5행 5열, 총 25개 변수에 난수 입력
while(input != 0) {
for(int z = 0 ; z < 5 ; z++) {
for(int z2 = 0 ; z2< 5 ; z2++) {
if(input == arr[z][z2]) {
arr[z][z2] = 0;
}System.out.print(arr[z][z2] + "\t");
}
System.out.println();
}
System.out.print("1~25의 숫자를 입력하세요(종료 : 0) = ");
input = sc.nextInt();
}
}
--------------------------------// 같은 숫자 입력 시 0으로 변경 -> while문으로 반복
결과 값