Kosta 클라우드 네이티브 애플리케이션 개발 (CNA) 교육 12일차

2024. 12. 3. 17:09Kosta 클라우드 네이티브 어플리케이션 개발(CNA) 교육

JAVA 

Array

 

public class chap05_arr {

	/*
	 * 메모리 stack 영역 : 메서드 호출 및 실행 처리 LIFO : main 메서드가 가장 먼저 실행됨으로 가장 바닥에 있다.
	 * 
	 */

	public static void myprint3(char[] param) {

		for (char c : param) {
			System.out.println("호출 : " + c);
		}

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

		for (int i = 0; i < param.length; i++) {
			System.out.println("호출 : " + param[i]);
		}
	}

	public static void main(String[] args) {

		chap05_arr arrs = new chap05_arr();
		arrs.arr();
		arrs.arr2();
		arrs.quiz();
		arrs.quiz2();
		arrs.quiz3();
		arrs.quiz4();

	}
	
	
	public void quiz4() {
		
		// ---------------------------------------------------
		// QUIZ : 두개의 주사위와 동전 한개를 동시에 던질 경우 
		//        동전이 앞면이면서 주사위 두 눈의 합이 6인 경우는 언제인가?
		// <출력패턴>
		//	1,5  2,4  .... 5,1
		// ---------------------------------------------------
		
		// 0 앞면, 1 뒷면
		
		for(int c = 0; c < 2; c++) {
			for(int i = 1; i <= 6; i++ ) {
				for(int j = 1; j <= 6; j++) {
					if(i+j == 6 && c == 0)
						System.out.print("{ " + i +", "+ j + " }" +"  ");
				}
			}
		}
		
		
	}

	
	public void quiz3() {
		
		// QUIZ : 1 + -2 + 3 + -4 + ... 누적합이 100 이상이 될때는 언제인가? 
		// <출력패턴>
		//	1 + -2 + 3 + -4  ... 24
		//  누적합 : 100???
		// ---------------------------------------------------
	

		
		int i = 1;
		int sum = 0;
		while(true) {
			
			System.out.print(i%2==0 ? " + "  + -i + " + " : i );		
			sum = (i%2==0) ? sum - i : sum + i;				
			
//		if(i%2 ==0) {
//			sum-=i;
//			System.out.print("+ " + -i + " + ");
//		} else {
//			sum+=i;
//			System.out.print(i);
//		}
			
			if(sum >=100) 
				break;		
			i++;	
		}		
		
		System.out.println();
		
		System.out.println("sum = "+sum + ", count = " + i);
		
		System.out.println("--------------------------");
	
		
	}
	
	public void quiz2() {
		
		int sum = 0;
		
		for(int i = 1; i< 21; i++) {		
			if(i%2 != 0 && i%3 != 0) {			
				sum+=i;
			}			
		}
		System.out.println(sum);
		
		System.out.println("-------------------------------");
		
		
		System.out.println(IntStream.range(1, 21).filter(i -> i% 2 != 0 && i%3 != 0).sum());
	}
	
	

	public void quiz() {

		int[][] scores = { 
				{ 90, 94, 92 }, 
				{ 80, 84, 82 },
				{ 70, 74, 72 }, 
				{ 60, 64, 62 }
				};

		
		System.out.println(scores[1]);
		System.out.println("------------------------------------------");

		int korean = 0;
		int math = 0;
		int english = 0;
		
		for(int i = 0; i< scores.length; i++) {
			
			int sumNum = 0;
			for(int j = 0; j< scores[i].length; j++) {
				
				if(j == 0) {
					korean+=scores[i][j];
				} else if(j == 1) {
					math+=scores[i][j];
				} else {
					english+=scores[i][j];
				}
				
				sumNum+=scores[i][j];
				System.out.print(scores[i][j] + " ");
				
				}
			
			System.out.println(sumNum);	
			}
		System.out.println(korean+" "+ math+ " "+ english);
	}
	


	public void arr2() {
		char[] c = new char[] { 'A', 'B', 'C' };

//		myprint3(c);

		int[][] scores = { { 90, 94, 92 }, { 80, 84, 82 }, { 70, 74, 72 }, { 60, 64, 62 } };

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

		// 가변배열
		// int[][] score = new int[4][]; -> 값을 넣지 않음 -> 마음대로 입력 가능

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

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

		for (int[] row : scores) {
			for (int score : row) {
				System.out.print("score = " + score + "\t");
			}
			System.out.println();
		}

	}

	public void arr() {
		// 배열이 메모리에 올라가면 개수만큼 옆으로 늘어남 -> 메모리 탐색 효율 매우 좋음

//		int[] num1; // 개추
//		int num2[]; // 비추

		int[] num3; // 배열 선언
		num3 = new int[3]; // 배열 생성

		int[] num4 = new int[4]; // 선언과 동시에 생성

		int[] arr = { 1, 2, 3, 4, 5 };

		// 인덱스(index) : 배열 n번째, 0부터 시작
		// 인덱스 == 배열 크기

		for (int ar : arr) {
			System.out.println(ar);
		}

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

		/*
		 * Arrays.stream(arr).forEach(System.out::println);
		 * 
		 * System.out.println(args);
		 * 
		 * System.out.println(
		 * "---------------------------------------------------------");
		 * 
		 * IntStream.range(1, 10).filter(i -> i % 2 == 1).forEach(System.out::println);
		 * 
		 * System.out.println(
		 * "---------------------------------------------------------");
		 * 
		 */
	}
}