[Day11] Java 11 [8/23]

 

1.  ์—ฐ์›” ์ž…๋ ฅ๋ฐ›์•„์„œ ๋‹ฌ๋ ฅ์ถœ๋ ฅ

package days11;

import java.util.Scanner;

import days08.Ex04_02;

/**
 * @author k≡n¡k
 * @date 2022. 8. 23. - ์˜ค์ „ 10:08:53
 * @subject  ๋งŒ๋…„๋‹ฌ๋ ฅ[ ์‹œํ—˜ ]
 * @content   ๋…„๋„,์›” ์ž…๋ ฅ
 *
 */
public class Ex02_02 {
	 
	public static void main(String[] args) {
		 
		int year, month;  
		
		int [] ym = getYearMonth();		
		year = ym[0];
		month = ym[1]; 
		
		printCalendar(year, month);
	
	
	} // main
	
	private static void printCalendar(int year, int month) { 
		int dayOfWeek = getDayOfWeek(year, month);	  // 0~6 ์ •์ˆ˜
		int lastDay = getLastDay(year, month); 
		// ใ„ท. ๋‹ฌ๋ ฅ ์ถœ๋ ฅ
		System.out.printf("\t\t%d๋…„ %d์›”\n", year, month);
		String week = "์ผ์›”ํ™”์ˆ˜๋ชฉ๊ธˆํ† ";
		for (int i = 0; i < week.length() ; i++) {
			System.out.printf("\t%c", week.charAt(i));
		}
		System.out.println();
		// ๋‹ฌ๋ ฅ 1~ ๋งˆ์ง€๋ง‰๋‚ ์งœ ์ถœ๋ ฅ.
		// ๊ณต๋ฐฑ for
		for (int i = 0; i < dayOfWeek; i++) {
			System.out.print("\t");			
		}
		// ASCII ์ถœ๋ ฅ
		// ํ•œ ๋ผ์ธ์— 10๊ฐœ์”ฉ
		// 10๋ผ์ธ ์ถœ๋ ฅ ์ผ์‹œ ๋ฉˆ์ถค + ์—”ํ„ฐ
		for (int i = 1; i <=lastDay ; i++) {
			System.out.printf("\t%d", i);
			// ๊ฐœํ–‰
			if( ( i+ dayOfWeek )  % 7 == 0) System.out.println(); 
		}		
		System.out.println(); // ๊ฐœํ–‰
	}  
	
    public static int getDayOfWeek(int year, int month ) { 
    	int totalDays = getTotalDays(year, month, 1 );
    	int dayOfWeek = totalDays % 7 ; 
        return dayOfWeek;
    }
    
    private static int getTotalDays(int year, int month, int day) {    	 
    	int totalDays = 0;
    	totalDays = 365 * ( year-1 ) + ( year-1)/4 - ( year-1)/100 + ( year-1)/400;    	
    	for (int i = 1; i < month; i++) 			totalDays +=  getLastDay(year, i);     	
    	totalDays += day;
		return totalDays;
	}
 
    public static int getLastDay(int year, int month ) {    	  
    	int [] months = {31,28,31,30,31,30,31,31,30,31,30,31};    	
    	return  month==2 && Ex04_02.isLeapYear(year) ? 29 :   months[ month-1 ]; 
    }
  
	public static int [] getYearMonth() { 
		int [] ym = new int[2];   
		Scanner scanner = new Scanner(System.in);
		System.out.print("๋…„๋„ ์›” ์ž…๋ ฅ ? ");
		ym[0] = scanner.nextInt();
		ym[1] = scanner.nextInt();  
		return ym;
	}
	
} // class

2.  ๋ฐฐ์—ด

1) ์ธ๋ฑ์Šค ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ

2) ์ฐธ์กฐํ˜• ์ž๋ฃŒํ˜•

3) ์‹ค์ œ ๊ธฐ์–ต๊ณต๊ฐ„์€ new์—ฐ์‚ฐ์ž์— ์˜ํ•ด ๋™์ (heap)์˜์—ญ์— ํ• ๋‹น๋˜๊ณ  ์ฃผ์†Œ๋ฅผ ์Šคํƒ(stack)์˜์—ญ์—์„œ ์ฐธ์กฐ

4) ์ •์˜ : ๋™์ผํ•œ ์ž๋ฃŒํ˜•์„ ๋ฉ”๋ชจ๋ฆฌ์ƒ์— ์—ฐ์†์ ์œผ๋กœ ๋†“์ด๊ธฐ ํ•œ ๊ฒƒ

5) ์„ ์–ธ : ์ž๋ฃŒํ˜• [] ๋ฐฐ์—ด๋ช… = new ์ž๋ฃŒํ˜• [๋ฐฐ์—ดํฌ๊ธฐ]

int [] kors = new int[5000];

6) ๋ฐฐ์—ดํฌ๊ธฐ : kors.length

7) ๋ฐฐ์—ด์€ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ

8) ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•

int [] m = new int[3];
m[0] = 10;
m[1] = 20;
m[2] = 30;
int [] m = new int[]{1,2,3};
int [] m = {1,2,3};

3.  N์ฐจ์› ๋ฐฐ์—ด

1) 2์ฐจ์› ๋ฐฐ์—ด

int [][] m = new int[2][4]

์ดˆ๊ธฐํ™”

int [][] m = new int[][] {  {1,2,3,4}, {5,6,7,8} };
int [][] m =  {  
				                 {1,2,3,4}, 
				                 {5,6,7,8} 
		                     };

2) 3์ฐจ์› ๋ฐฐ์—ด

int [][][] m = new int[2][3][4];

์ดˆ๊ธฐํ™”

int [][][] m = {
				{
					{1,2,3,4},
					{5,6,7,8}
				},
				{
					{ 11,12,13,14},
					{ 15,16,17,18}
				}
		};
public static void main(String[] args) {		
		int [] m  = new int[3]; 
		int index = 0;
		char con = 'y'; //	'n'
		Scanner scanner = new Scanner(System.in);
		
		do {
			
			// ๋ฐฐ์—ด ํฌ๊ธฐ ๋‹ค ์ฐผ๋Š”์ง€ 
			if(  m.length   ==   index ) {
				int [] temp = new int[m.length + 3];
				// m -> temp ๋ณต์‚ฌ
				System.arraycopy(m, 0, temp, 0, m.length);
				
				m = temp;
			}
			
			System.out.printf("> m[%d]์˜ ์ •์ˆ˜ ์ž…๋ ฅ ? ", index);
			m[index++] = scanner.nextInt();
			 
			System.out.print("> ์ž…๋ ฅ ๊ณ„์†ํ•ฉ๋‹ˆ๊นŒ? ");
			con = scanner.next().charAt(0);  // "y" -> 'y'
		} while (   ( con == 'y' || con =='Y' )    );
		
		
		 
		System.out.println( Arrays.toString( m ) );
	} // main

๋ฐฐ์—ด ๋ณต์‚ฌ

System.arraycopy(m, 0, temp, 0, m.length);
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ