Backend/JAVA2 멘토시리즈

JAVA Eclipse 14 배열 : 다차원배열

쏠솔랄라 2023. 4. 4. 07:51

 

 

다차원배열

 

 

다차원 배열의 정의

: 배열의 각 요소가 배열인 배열

 

 

2차원 배열의 선언

자료형[ ][ ] 배열명;

 

2차원 배열의 생성

배열명 = new  자료형[크기][크기];

int arr[][] = new int [3][4];

-> 4개짜리 배열의 묶음이 3개

 

 

2차원 배열의 인덱스

2차원 배열은 행과 열로 구성되어 있다

배열은 변수를 더 편리하게 관리하기 위한 목적으로 만든 구조

 

2차원 배열의 각 변수의 이름

 

 

2차원 배열의 초기화

배열을 생성하자마자 값을 넣어 주는 것

 

자료형 배열명 [ ] [ ] = {
{값1, 값2, 값3, ...},
{값4, 값5, 값6, ...},
{값7, 값8, 값9, ...},
...
};1

 

 

ex1.

 

학생 4명의 국어, 영어, 수학 점수를 입력받아 출력

 

import java.util.Scanner;

public class Student {

public static void main(String[] args) {

 

int scores[][] = new int[4][3];

String subject[] = {"국어", "영어", "수학"};

 

Scanner sc = new Scanner (System.in);

 

// 2차원 배열을 활용해 사용자에게 입력받은 값을 저장하는 방법

for (int j=0; j<4; j++) {

System.out.println(j+1 + "번 학생 점수 ----------");

for (int i=0; i<3; i++) {

System.out.print(subject[i] + ": ");

scores[j][i] = sc.nextInt();

}

}

 

// 2차원 배열에 저장된 값을 출력

 

for (int i=0; i<3; i++) {

System.out.print("\t" + subject[i]);

}

 

System.out.println();

for (int j=0; j<4; j++) {

System.out.print(j+1 + "번:\t");

for (int i=0; i<3; i++) {

System.out.print(scores[j][i] + "\t");

}

System.out.println();

}

}

}

 

출력화면

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93

 

JAVA Eclipse 09 제어문 : 반복문 for

https://developernew.tistory.com/102

 

JAVA Eclipse 10 제어문 : 반복문 while, do-while

https://developernew.tistory.com/103

 

JAVA Eclipse 11 제어문 : 반복문의 break, continue

https://developernew.tistory.com/106

 

JAVA Eclipse 12 배열 : 배열의 개념 및 사용

https://developernew.tistory.com/107

 

JAVA Eclipse 13 배열 : 예제풀이, 로또번호 생성기

https://developernew.tistory.com/115