Notice
Recent Posts
Recent Comments
Link
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

스포츠마케터의 초보 개발자 도전기

JAVA study 07 본문

develop/JAVA

JAVA study 07

teammate brothers 2024. 3. 6. 15:44

multi_array 다차원 배열

package ex1_multi_array;

public class Ex1_multi_array {
	public static void main(String[] args) {

		// 다차원 배열
		// 1차원 배열이 n개 모이면, n차원 배열

		// new int[1차원 배열의 수][각 1차원배열의 index의 수]
		// new int[큰방][작은방]

		int[][] test = new int[2][3];

		test[0][0] = 100;
		test[0][1] = 200;
		test[0][2] = 300;

		test[1][0] = 400;
		test[1][1] = 500;
		test[1][2] = 600;

		System.out.println(test[0][2]); // 300

		System.out.println("-----------------------");

		// test 배열이 가진 모든 값을 출력 해 보자.
		for (int i = 0; i < test.length; i++) {

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

				System.out.print(test[i][j] + " ");

			} // inner

			System.out.println();

		} // outer

	}// main

}
package ex1_multi_array;

public class Ex2_multi_array {
	public static void main(String[] args) {

		// int[] arr = {10, 20, 30};
		char[][] cArr = { { 'A' }, { 'B', 'C', 'D' }, { 'E', 'F' } };

		for (int i = 0; i < cArr.length; i++) {
			for (int j = 0; j < cArr[i].length; j++) {
				System.out.print(cArr[i][j] + " ");

			} // inner
			System.out.println();
		} // outer

	}// main
}
package ex1_multi_array;

public class Ex3_multiArray {
	public static void main(String[] args) {

		String[][] str = new String[2][];
		str[0] = new String[3];
		str[1] = new String[2];

		str[0][0] = "Java";
		str[0][1] = "Jsp";
		str[0][2] = "Script";

		str[1][0] = "jQuery";
		str[1][1] = "html";
		// str[1][2] = "A"; <--배열에 존재하진 않는 index는 접근 불가

		for (int i = 0; i < str.length; i++) {

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

				System.out.print(str[i][j] + "\t");
			}
			System.out.println();
		}

	}// main

}

 

예제1) 주어진 multiArray에서 3의 배수만 출력

package ex2_muli_array_work;

public class Ex1_work {
	public static void main(String[] args) {
		
		//3의 배수만 출력
		
		int[][] array = { {1, 2, 3},
						  {4, 5, 6, 7, 8,},
						  {9, 10, 11, 12, 13} };
		
		for (int i = 0; i < array.length; i++ ) {
			for (int j = 0; j < array[i].length; j++) {
				if (array[i][j] % 3 !=0) {
					
					System.out.print(array[i][j] + " ");
				}
			}
		}
		
		
	}//main
}

 

예제2) multiArray에 담긴 모든값의 합과 평균을 출력

package ex2_muli_array_work;

public class Ex2_work {
	public static void main(String[] args) {
		
		//arr에 담긴 모든 값의 합과 평균을 출력
		//------------
		//총합 : 89
		//평균 : 6.4
		
		int[][] arr = { {4, 2, 6},
				{11, 15, 7, 1},
				{3, 17, 2, 5, 9},
				{3, 4} };
		
		int sum = 0;
		int arrNum = 0;
		for (int i = 0; i < arr.length; i++) {

			for (int j = 0; j < arr[i].length; j++) {
			
				sum += arr[i][j];
				arrNum++;
		
			}//inner
		}//outer
		
		System.out.printf("총합은 : %d\n", sum);
		float avg = (float) sum / arrNum;
		System.out.printf("평균 : %.1f", avg);
		
	}//main
}

 

예제3) multiArray에서 5를 초과하는 첫번째 값을 찾아 출력

package ex2_muli_array_work;

public class Ex3_work {
	public static void main(String[] args) {
		
		//arr배열에서 5을 초과하는 첫번째 값을 찾아 출력
		
		
		int[][] arr = { {4, 2, 6},
						{11, 15, 7, 1},
						{3, 17, 2, 5, 9} };
		
		abc : for (int i = 0; i < arr.length; i++) {

				for (int j = 0; j < arr[i].length; j++) {
			
					if ( arr[i][j] > 5) {
					System.out.println(arr[i][j]);

					break abc;
				}
						
			}//inner
		}//outer
		
		
	}//main
}

 

예제4) 찾을 값을 입력하고, multiArray에서 입력값을 초과하는 첫번째 값을 출력. 해당행의 모든 값을 출력.

package ex2_muli_array_work;

import java.util.Scanner;

public class Ex4_work {
	public static void main(String[] args) {
		
		//찾을 값 입력
		//입력값을 초과하는 첫번째 값을 찾고
		//해당행의 모든 행의 값을 출력
		
		
		int[][] arr = { {3, 4, 6, 9},
						{5, 21, 11, 15, 13},
						{8, 1, 20, 14},
						{4, 10, 15, 16} };
		
		
		Scanner sc = new Scanner(System.in);
		System.out.print("찾고싶은 값 입력 : ");
		int a = sc.nextInt();
				
		abc: for(int i = 0; i < arr.length; i++) {
			
				for(int j = 0; j < arr[i].length; j++) {
					if (arr[i][j] > a) {
						
						System.out.println(a + "보다 큰 첫번째 수 : " + arr[i][j]);
						
						for(int k = 0; k < arr[i].length; k++) {
						
							System.out.print(arr[i][k] + " ");
						}
						break abc;
					
					}
				
				}//inner
			
			}//outer
	
	}//main
}

 

과제1) 값출력

package ex3_work;

public class Ex1_work {
	public static void main(String[] args) {
		
		//2차원 배열을 이용해 다음의 결과를 출력하시오
		//0 1 2 3
		//1 2 3 4
		//2 3 4 5
		//3 4 5 6
		
		
		int [][] arr = new int[4][4];
		
				
		for (int i = 0; i < arr.length; i++) {
			
			for(int j = 0; j <arr[i].length; j++) {
				arr[i][j] += j;
				arr[i][j] += i;
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
		
	}//main
}

 

과제2) lotto 출력

package ex3_work;

import java.util.Random;

public class Ex2_work_lotto {
	public static void main(String[] args) {

		// 1~45사이의 난수를 발생시켜 lotto배열에 담고 결과를 출력

		int[] lotto = new int[6];

		// 랜덤으로 겹치지 않는 로또번호 추출
		for (int i = 0; i < lotto.length; i++) {

			lotto[i] = new Random().nextInt(45) + 1;

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

				if (lotto[i] == lotto[j]) {
					i--;
				}
			} // inner

		} // outer
		for (int i = 0; i < lotto.length; i++) {

			System.out.print(lotto[i] + " ");
		}

	}// main

}

 

과제2-1) lotto 출력 teacher's code

		out : for(int i = 0; i < lotto.length; ) {
		
			lotto[i] = new Random().nextInt(45) + 1;
			
			for(int j = 0; j < i; j++) {
			
				if (lotto[i] == lotto[j]) {
					continue out;
				}
			}//inner
			
			System.out.print(lotto[i] + " ");
			i++;
		}//outer

 

예제) 학생들의 과목 성적을 등록하고 출력하는 프로그램

package ex3_work;

import java.util.Scanner;

public class Ex3_work {
	public static void main(String[] args) {

		// 학생들의 수학, 영어 성적을 등록하고 출력하는 프로그램을 제작

		// 등록할 인원 수 : n
		// 이름 : hong
		// 수학 : 90
		// 영어 : 87
		// ---------------
		// 이름 : kim
		// 수학 : 70
		// 영어 : 100
		// 2명등록 완료
		// hong 90 87
		// kim 70 100

		Scanner sc = new Scanner(System.in);

		System.out.print("등록인원을 입력하세요 : ");
		int studentNum = sc.nextInt();

		String[][] score = new String[studentNum][3];

		for (int i = 0; i < score.length; i++) {

			System.out.printf("%d 학생의 이름을 입력하세요 : ", i + 1);
			String name = sc.next();
			score[i][0] = name;
			System.out.printf("%d 학생의 수학성적을 입력하세요 : ", i + 1);
			String math = sc.next();
			score[i][1] = math;
			System.out.printf("%d 학생의 영어성적을 입력하세요 : ", i + 1);
			String english = sc.next();
			score[i][2] = english;

		} // inner

		System.out.println();

		System.out.println(studentNum + "명 등록완료");

		for (int i = 0; i < score.length; i++) {

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

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

			}

		}

	}// main
}

 

예제) 학생들의 과목 성적을 등록하고 출력하는 프로그램 tCode

package ex3_work;

import java.util.Scanner;

public class Ex3_work_tCode {
	public static void main(String[] args) {

		// 학생들의 수학, 영어 성적을 등록하고 출력하는 프로그램을 제작

		// 등록할 인원 수 : n
		// 이름 : hong
		// 수학 : 90
		// 영어 : 87
		// ---------------
		// 이름 : kim
		// 수학 : 70
		// 영어 : 100
		// 2명등록 완료
		// hong 90 87
		// kim 70 100

		Scanner sc = new Scanner(System.in);

		System.out.print("등록인원을 입력하세요 : ");
		int n = sc.nextInt();

		String[] info = { "이름", "수학", "영어" };
		String[][] stu = new String[n][info.length]; // n명만큼 / info의 갯수만큼 정보 입력 받음 -> 유지보수 편하게

		// 값을 입력 받아 stu배열에 저장
		for (int i = 0; i < n; i++) { // n대신 stu.length 사용 가능

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

				System.out.printf("%s : ", info[j]);
				stu[i][j] = sc.next();

			} // inner
			System.out.println("-------------");

		} // outer
		System.out.println(n + "명 등록완료");
		System.out.println("이름\t수학\t영어"); // for문으로 자동화 가능하나 일단 이정도로 넘어감

		for (int i = 0; i < stu.length; i++) {

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

				System.out.print(stu[i][j] + "\t");
			}
			System.out.println();
		}

	}// main
}

'develop > JAVA' 카테고리의 다른 글

JAVA study 09  (0) 2024.03.08
JAVA study 08  (0) 2024.03.07
JAVA study 06  (0) 2024.03.05
JAVA study 05  (0) 2024.03.05
JAVA study 04  (0) 2024.03.05