[JAVA]/06.배열

07. 2차원배열_ 2 (배열의 초기화)

76beny 2022. 4. 6. 22:07

목차

- 2차원배열 초기화_인덱스이용

- 2차원배열 초기화_for문이용

- 2차원배열 초기화_선언과 동시에 이용

- 2차원배열 예시


2차원 배열 초기화_ 인덱스이용

arr[0][0] = 1;
arr[1][1] = 2;

 


2차원 배열 초기화_ for문 이용

for(int i = 0; i < arr.length; i++) {
	for(int j = 0; j < arr[i].length; j++) {
		arr[i][j] = j;
	}
}

 


2차원 배열 초기화_ 선언과 동시에 이용

int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}}; //많이 씀! - new int[][] 생략!
int[][] arr = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}};
int[][] arr={
			{1,2,3,4},
			{5,6,7,8}
			};   //배열과 초기화를 동시에! 이렇게도 씀. (좀 더 직관적)


String fruit[][] = {{"사과", "딸기", "석류"},
					{"바나나", "참외", "레몬"}};

 


2차원 배열 초기화_선언 예시

 

import java.util.Arrays;

public class DoubleArrayBasic {	
	public static void main(String[] args) {
        //3.2차원 배열 초기화+생성
		int[][] array3 = new int [][] { {1,2,3,4},
						{2,4},// 가변선언가능
						{4,3,2,1},};
										
//		//내부type을 모두 명시한 문법	(위에를 더 많이씀)			
//		int[][] array4 = new int [][] {new int[]{1,2,3,4},
//						new int[]{2,4},
//						new int[]{4,3,2,1},};
										
										
										
		for(int i = 0 ; i < array3.length;i++) {
			for(int j = 0 ; j < array3[i].length;i++) {
				System.out.println(array3[i][j]+ " ");
			}
			System.out.println();	
			//출력값
//			1 2 3 4
//			2 4 
//			4 3 2 1
		}
		
		
	}
	
}

2차원 배열 예시

■ 예시 1

class Ex5_8 {
	public static void main(String[] args) {
		int[][] score = {
				  { 100, 100, 100 }
				, { 20, 20, 20 }
				, { 30, 30, 30 }
				, { 40, 40, 40 }
// 이게 4행3열 이므로 원래는 new int[4][3]
//score.length=4, score[i].length=3
		};

		int sum = 0;

		for (int i = 0; i < score.length; i++) {//score.length=4(스코어의 길이 0,1,2,3) 
			for (int j = 0; j < score[i].length; j++) {
            //j는 0,1,2, score[i].length=3=i행의 열개수(길이)
			//score[i].length=>score[0].length 일경우
            //=>score[0][0],score[0][1],score[0][2]=>3개
            
				System.out.printf("score[%d][%d]=%d%n", i, j, score[i][j]);

				sum += score[i][j];
			}
		}

		System.out.println("sum=" + sum);
	}
}
//score[0][2]=100
//score[1][0]=20
//score[1][1]=20
//score[1][2]=20
//score[2][0]=30
//score[2][1]=30
//score[2][2]=30
//score[3][0]=40
//score[3][1]=40
//score[3][2]=40
//sum=570  출력

 

■ 예시2

import java.util.Scanner;

class Ex5_10{
	public static void main(String[] args) {
		String[][] words = {
			{"chair","의자"},      	// words[0][0], words[0][1]
			{"computer","컴퓨터"}, 	// words[1][0], words[1][1]
			{"integer","정수"}     	// words[2][0], words[2][1]
		};

		Scanner scanner = new Scanner(System.in);

		for(int i=0;i<words.length;i++) {
			System.out.printf("Q%d. %s의 뜻은?", i+1, words[i][0]);
			//Q%d(ouble)정수, %S(tring) 문자열 출력지시자

			String tmp = scanner.nextLine();

			if(tmp.equals(words[i][1])) {
				System.out.printf("정답입니다.%n%n");
			//%n은 printf에서 줄바꿈 기능으로 쓰임(\n도 같지만 os마다 틀려서 공통인%n사용
			} else {
			   System.out.printf("틀렸습니다. 정답은 %s입니다.%n%n",words[i][1]);
			}
		} // for
	} // main의 끝
}

 


"본 인터넷 사이트 내의 모든 이미지, 문구, 콘텐츠, 내용 등에 대한 저작권은 76beny에게 있습니다.

이를 무단으로 도용, 복사, 전재, 재배포, 2차 변형 등을 할 경우

민, 형사상 법적 조치 등 저작권법에 의거하여 처벌 받을 수 있습니다."