728x90
๋ฐ˜์‘ํ˜•

1. if ๋ฌธ

1) if ๋ฌธ๊ณผ else ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

- if ๋ฌธ : ์ฃผ์–ด์ง„ ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ

- else ๋ฌธ: if ๋ฌธ์˜ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ

if (์กฐ๊ฑด๋ฌธ) {
	<์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ>;
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ2>;
} else {
	<์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ3>;
    <์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ4>;
}

 

2) ๋น„๊ต ์—ฐ์‚ฐ์ž

๋น„๊ต ์—ฐ์‚ฐ์ž ์„ค๋ช…
x <  y x๊ฐ€ y๋ณด๋‹ค ์ž‘๋‹ค
x > y x๊ฐ€ y๋ณด๋‹ค ํฌ๋‹ค
x == y x์™€ y๊ฐ€ ๊ฐ™๋‹ค
x != y x์™€ y๊ฐ€ ๊ฐ™์ง€ ์•Š๋‹ค
x >= y x๊ฐ€ y๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค
x <= y y์‚ฌ x๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค
// ๋ˆ์ด 3000์› ์ด์ƒ ์žˆ์œผ๋ฉด ํƒ์‹œ, ์•„๋‹ˆ๋ฉด ๊ฑท๊ธฐ
int money = 2000;
if (money > 3000) {
    System.out.println("ํƒ์‹œ");
} else {
    System.out.println("๊ฑท๊ธฐ");
}
 // ๊ฑท๊ธฐ

 

3) and, or, not ์—ฐ์‚ฐ์ž

์—ฐ์‚ฐ์ž ์„ค๋ช…
x && y x์™€ y ๋ชจ๋‘ ์ฐธ์ด์–ด์•ผ ์ฐธ
x || y x์™€ y ๋‘˜ ์ค‘์—์„œ ์ ์–ด๋„ ํ•˜๋‚˜๊ฐ€ ์ฐธ์ด๋ฉด ์ฐธ
!x x๊ฐ€ ๊ฑฐ์ง“์ด๋ฉด ์ฐธ
// ์นด๋“œ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ๋ˆ์ด3000์› ์ด์ƒ ์žˆ์œผ๋ฉด ํƒ์‹œ, ์•„๋‹ˆ๋ฉด ๊ฑท๊ธฐ
int money = 2000;
boolean card = true;
if (money > 3000 || card) {
    System.out.println("ํƒ์‹œ");
} else {
    System.out.println("๊ฑท๊ธฐ");
}

// ํƒ์‹œ

 

4)  contains

- ํ•ด๋‹น ์•„์ดํ…œ์ด ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ / ์กฐ๊ฑด๋ฌธ์— ๋งŽ์ด ํ™œ์šฉ๋œ๋‹ค

import java.util.ArrayList;

public class money {
	public static void main(String[] args) {
	ArrayList<String> pocket = new ArrayList<String>();
	pocket.add("paper");
	pocket.add("cellphone");
	pocket.add("money");
	
	if (pocket.contains("money")) {
		System.out.println("ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ");
	} else {
		System.out.println("๊ฑธ์–ด๊ฐ€๋ผ");
	}
	
	}
}

// ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ

 

5) else if

- ์žˆ๋‹ค์™€ ์—†๋‹ค์ค‘์— ์—†์„ ๊ฒฝ์šฐ ๋‹ค์‹œ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ„์–ด์ฃผ๋Š” ์กฐ๊ฑด๋ฌธ์— ํ•ด๋‹นํ•œ๋‹ค

(if, else๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉ๋˜์–ด ํ•œ ๋ฒˆ์— ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๊ณ  ์‚ฐ๋งŒํ•œ ๋Š๋‚Œ์ด๋“œ๋Š๋ฐ... ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ... ์ž๋ฐ”๋Š” ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” else if ๋ฌธ์ด ์žˆ๋‹ค)

package test;
import java.util.ArrayList;

public class money {
	public static void main(String[] args) {
	boolean hasCard = true;
	ArrayList<String> pocket = new ArrayList<String>();
	pocket.add("paper");
	pocket.add("cellphone");
	
	if (pocket.contains("money")) {
		System.out.println("ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ");
	} else {
		if (hasCard) {
			System.out.println("ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ");
		}else {
			System.out.println("๊ฑธ์–ด๊ฐ€๋ผ");
		}
	}
	
	}
}

// ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ

 

์•„๋ž˜ else if ๋ถ€๋ถ„๋“ค์„ ์ˆ˜์ •ํ•˜๋ฉด ์กฐ๊ธˆ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜๋กœ ๋‚˜ํƒ€๋‚ด์งˆ ์ˆ˜ ์žˆ๋‹ค / else if๋Š” ๊ฐœ์ˆ˜์ œํ•œ ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ

	if (pocket.contains("money")) {
		System.out.println("ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ");
	}else if (hasCard) {
		System.out.println("ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋ผ");
	}else {
		System.out.println("๊ฑธ์–ด๊ฐ€๋ผ");
	}

 

2. switch / case ๋ฌธ

: if ๋ฌธ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ์ข€ ๋” ์ผ์ •ํ•œ ํ˜•์‹์ด ์ž‡๋Š” ์กฐ๊ฑด๋ฌธ..

=> ์ž…๋ ฅ ๋ณ€์ˆ˜์˜ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” case ์ž…๋ ฅ๊ฐ’(์ž…๋ ฅ๊ฐ’1, ์ž…๋ ฅ๊ฐ’2,....) ์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น case ๋ฌธ์— ์†ํ•œ ๋ฌธ์žฅ๋“ค์ด ์‹คํ–‰๋œ๋‹ค...

  break ๋ฌธ ๋˜ํ•œ ์กด์žฌ(ํ•ด๋‹น case๋ฌธ์„ ์‹คํ–‰ํ•œ ๋’ค swirch ๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€๊ธฐ ์œ„ํ•จ), break ๋ฌธ์ด ์—†๋‹ค๋ฉด, ๋‹ค์Œ case๋กœ ์ด๋™

 

package test;
import java.util.ArrayList;

public class money {
	public static void main(String[] args) {
		int month = 8;
		String monthString = " ";
		switch(month) {
		case 1: monthString = "January";
		break;
		case 2: monthString = "February";
		break;
		case 3: monthString = "March";
		break;
		case 4: monthString = "April";
		break;
		case 5: monthString = "May";
		break;
		case 6: monthString = "June";
		break;
		case 7: monthString = "July";
		break;
		case 8: monthString = "August";
		break;
		case 9: monthString = "September";
		break;
		case 10: monthString = "October";
		break;
		case 11: monthString = "November";
		break;
		case 12: monthString = "December";
		break;
		default: monthString = "Invalid month";
		break;
		}
		System.out.println(monthString);
	}
}

ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž์— ๋Œ€ํ•œ case๊ฐ€ ์‹คํ–‰๋˜์—ˆ๋‹ค!!

๋งŒ์•ฝ month๊ฐ€ 1์—์„œ 12์‚ฌ์ด์˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฐ’์ด ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๋ฉด defaul: ๋ฌธ์žฅ์ด ์ˆ˜ํ–‰๋œ๋‹ค!

๊ฐ’์ด ์ •ํ˜•ํ™”๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ if ๋ฌธ๋ณด๋‹ค๋Š” switch/case ๋ฌธ์„ ์“ฐ๋Š” ๊ฒƒ์ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค!!

 

3. while ๋ฌธ

: ์กฐ๊ฑด๋ฌธ์ด ์ฐธ์ธ ๋™์•ˆ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ๋“ค์„ ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค

package test;

public class tree {
	public static void main(String[] args) {
		int treeHit = 0;
		while (treeHit < 10) {
			treeHit++; // treeHit += 1; ๋กœ๋„ ํ‘œํ˜„๊ฐ€๋Šฅ
			System.out.println("๋‚˜๋ฌด๋ฅผ " + treeHit + "๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.");
			if (treeHit == 10) {
				System.out.println("๋‚˜๋ฌด ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.");
			}
		}

	}
}

/*
๋‚˜๋ฌด๋ฅผ 1๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 2๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 3๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 4๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 5๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 6๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 7๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 8๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 9๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด๋ฅผ 10๋ฒˆ ์ฐ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‚˜๋ฌด ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
*/

while๋ฌธ์˜ ์กฐ๊ฑด๋ฌธ : treeHit < 10 (10๋ณด๋‹ค ์ž‘์€ ๋™์•ˆ ์ˆ˜ํ–‰) / +1 ์ปค์ง€๋ฉด์„œ ์ˆ˜ํ–‰ํ•˜๊ณ  / 10์ด ๋˜๋ฉด์„œ ์ข…๋ฃŒ.. while ๋ฌธ ๋น ์ ธ๋‚˜์˜ด

 

 

1) ๋ฌดํ•œ ๋ฃจํ”„

: ๋ฌดํ•œํ•˜๊ฒŒ while ๋ฌธ ๋‚ด์˜ ๋ฌธ์žฅ๋“ค์„ ์ˆ˜ํ–‰... ๋น ์ ธ๋‚˜์˜ค์ง€ ๋ชปํ•œ๋‹ค...

package test;

public class loop {
	public static void main(String[] args) {
		while (true) {
			System.out.println("Ctrl-C๋ฅผ ๋ˆŒ๋Ÿฌ์•ผ while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.");
		}
	}
}

/*
Ctrl-C๋ฅผ ๋ˆŒ๋Ÿฌ์•ผ while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Ctrl-C๋ฅผ ๋ˆŒ๋Ÿฌ์•ผ while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Ctrl-C๋ฅผ ๋ˆŒ๋Ÿฌ์•ผ while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
...
*/

[Ctrl] + [c] : ์ข…๋ฃŒ

 

2) while ๋ฌธ ๋น ์ ธ๋‚˜๊ฐ€๊ธฐ - break

: while ๋ฌธ์„ ๊ฐ•์ œ๋กœ ๋ฉˆ์ถฐ์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค

package test;

public class loop {
	public static void main(String[] args) {
		int coffee = 10;
		int money = 300;
		
		while (money > 0) {
			System.out.println("๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.");
			coffee--;
			System.out.println("๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ " + coffee + "์ž…๋‹ˆ๋‹ค.");
			if (coffee == 0) {
				System.out.println("์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ํŒ๋งค๋ฅผ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.");
				break;
			}
		}
	}
}

/*
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 9์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 8์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 7์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 6์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 5์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 4์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 3์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 2์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 1์ž…๋‹ˆ๋‹ค.
๋ˆ์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ปคํ”ผ๋ฅผ ์ค๋‹ˆ๋‹ค.
๋‚จ์€ ์ปคํ”ผ์˜ ์–‘์€ 0์ž…๋‹ˆ๋‹ค.
์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ํŒ๋งค๋ฅผ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.
*/

๋ˆ์ด 0๋ณด๋‹ค ํด๋•Œ๋™์•ˆ ์ง„ํ–‰ํ•˜๋‹ค... 0์ด ๋˜์–ด๋ฒ„๋ฆฌ๋ฉด ์ถœ๋ ฅํ›„ break๊ฐ€ ํ˜ธ์ถœ๋˜์–ด while ๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค

 

3) while ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ - continue

: while ๋ฌธ ์•ˆ์˜ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์–ด๋–ค ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•ด์„œ ์ฐธ์ด ์•„๋‹Œ ๊ฒฝ์šฐ while ๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€๋Š” ๋Œ€์‹  while ๋ฌธ์˜ ๋งจ ์ฒ˜์Œ, ์ฆ‰ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ continue๋ฅผ ์‚ฌ์šฉ(๋น ์ ธ๋‚˜๊ฐ€์ง€ ์•Š๊ณ  ๋‹ค์‹œ ์˜ฌ๋ผ๊ฐ€์„œ ์ง„ํ–‰)

package test;

public class loop {
	public static void main(String[] args) {
		int a = 0;
		while (a < 10) {
			a++;
			if (a % 2 == 0) {
				continue; // ์ง์ˆ˜์ธ ๊ฒฝ์šฐ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.
			}
			System.out.println(a); // ํ™€์ˆ˜๋งŒ ์ถœ๋ ฅ๋œ๋‹ค
		}
	}
}

/*
1
3
5
7
9
*/

a๊ฐ€ ์ง์ˆ˜์ด๋ฉด continue๋ฌธ์ด ์‹คํ–‰๋˜์–ด while ๋ฌธ์˜ ๋งจ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ํ•œ๋‹ค!! 

๊ทธ๋Ÿฌ๋ฏ€๋กœ a๊ฐ€ ์ง์ˆ˜์ด๋ฉด System.out.println(a)๋Š” ์ˆ˜ํ–‰๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ™€์ˆ˜๋งŒ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค!!!!

 

4. for ๋ฌธ

: ๋ฌธ์žฅ์„ ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ์— ์‚ฌ์šฉ

 

1) for ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

// ๊ธฐ๋ณธ ๊ตฌ์กฐ
for (์ดˆ๊นƒ๊ฐ’; ์กฐ๊ฑด๋ฌธ; ์ฆ๊ฐ“๊ฐ’) {
	...
}

/////
String[] numbers = ["one", "two", "three"]
for (int i = 0; i < numbers.length; i++) :
	System.out.println(numvers[i]);
}
/*
one
two
three
*/

i์˜ ์ดˆ๊นƒ๊ฐ’์€ 0, ์กฐ๊ฑด๋ฌธ์€ i < numbers.length, ์ฆ๊ฐ“๊ฐ’์€ i++์ด ๋œ๋‹ค!!

=> i ๊ฐ’์ด numbers์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ์ž‘์€ ๋™์•ˆ ๊ณ„์† i ๊ฐ’์„ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ for ๋ฌธ์˜ ๋ฌธ์žฅ๋“ค์„ ์ˆ˜ํ–‰

 

2) for ๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€๊ธฐ - continue

: while ๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ ์šฉ!

int[] marks = {90, 25, 67, 45, 80};
for (int i = 0; i < marks.length; i++) {
	if (marks[i] < 60) {
    	continue; // ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.
    }
    System.out.println((i+1)+"๋ฒˆ ํ•™์ƒ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");
}

/*
1๋ฒˆ ํ•™์ƒ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.
3๋ฒˆ ํ•™์ƒ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.
5๋ฒˆ ํ•™์ƒ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.
*/

์ ์ˆ˜๊ฐ€ 60์  ๋ฏธ๋งŒ์ด๋ฉด continue๋ฌธ์ด ์‹คํ•ด๋˜์–ด ์ถ•ํ•˜๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์ด ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ , 

for ๋ฌธ์˜ ์ฒซ๋ถ€๋ถ„์œผ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค!!!

=> ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ, ํŠน์ • ์กฐ๊ฑด์—์„œ๋Š” ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๊ฑด๋„ˆ๋›ฐ์–ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!!!!

 

3) ์ด์ค‘ for ๋ฌธ

: for ๋ฌธ์„ ๋‘๋ฒˆ ์‚ฌ์šฉํ•œ ๊ฒƒ..

// ๊ตฌ๊ตฌ๋‹จ ๋งŒ๋“ค๊ธฐ
package test;

public class gugudan {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int i=2; i < 10; i++) {
			for (int j=1; j <10; j++) {
				System.out.print(i*j+" ");
			}
			System.out.println(""); // ์ค„์„ ๋ฐ”๊พธ์–ด ์ถœ๋ ฅํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค
		}
	}
}

/*
2 4 6 8 10 12 14 16 18 
3 6 9 12 15 18 21 24 27 
4 8 12 16 20 24 28 32 36 
5 10 15 20 25 30 35 40 45 
6 12 18 24 30 36 42 48 54 
7 14 21 28 35 42 49 56 63 
8 16 24 32 40 48 56 64 72 
9 18 27 36 45 54 63 72 81 
*/

๋‹จ์ด ๋ฐ”๋€”๋•Œ println์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค„๋ฐ”๊ฟˆํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜์˜€๋‹ค!!

 

5. for each ๋ฌธ

: ์ด์ „์— ๋ฐฐ์› ๋˜ for๋ฌธ์„ ํ™œ์šฉํ•œ ์˜ˆ์ œ(์ฒจ๋ด„)

// for each ๋ฌธ์˜ ๊ตฌ์กฐ
for (type ๋ณ€์ˆ˜๋ช…: iterate) {
	body-of-loop
}

iterate๋Š” ๋ฃจํ”„๋ฅผ ๋Œ๋ฆด ๊ฐ์ฒด์ด๊ณ  iterate ๊ฐ์ฒด์—์„œ ํ•œ ๊ฐœ์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ณ€์ˆ˜๋ช…์— ๋Œ€์ž…๋˜์–ด for ๋ฌธ์ด ์ˆ˜ํ–‰๋œ๋‹ค...

iterate์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์€ ๋ฃจํ”„๋ฅผ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•(๋ฐฐ์—ด์ด๋‚˜ ArrayList๋“ฑ)๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค...

+ ๋ณ€์ˆ˜๋ช…์˜ type(์ž๋ฃŒํ˜•)์€ iterate ๊ฐ์ฒด์— ํฌํ•จ๋œ ์ž๋ฃŒํ˜•๊ณผ ์ผ์น˜ํ•ด์•ผํ•œ๋‹ค!!!

 

String[] numbers = {"one", "two", "three"};
for(int i = 0; i < numbers.lengh; i++) {
	System.out.println(numbers[i]);
}

// for each ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด...
String[] numbers = {"one", "two", "three"};
for(String number: numbers) {
	System.out.println(number);
}

 

๋ฌธ์ œ...

1. everywhere

2. i % 3 == 0

3. i >5 / int j=0; j <5; j++;

4. int i = 1; i < 101; i++;

5. int mark: marks / total / marks.length;

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

6) ๋ฐฐ์—ด

 

๋ฐฐ์ ์˜ ๊ธธ์ด ์„ค์ •ํ•˜๊ธฐ

- ๋ฐฐ์—ด์€ ๋ฐ”๋กœ ์˜†์— []๊ธฐํ˜ธ๋ฅผ ๋ถ™์ธ๋‹ค

- ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋จผ์ € ์„ค์ •ํ•œ ๋’ค, ๋ฐฐ์—ด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋‚˜์ค‘์— ๋Œ€์ž…

String[] weeks = new String[7];
weeks[0] = "์›”";
weeks[1] = "ํ™”";
weeks[2] = "์ˆ˜";
weeks[3] = "๋ชฉ";
weeks[4] = "๊ธˆ";
weeks[5] = "ํ† ";
weeks[6] = "์ผ";
// ์ดˆ๊นƒ๊ฐ’ ์—†์ด ๋ฐฐ์—ด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” 7๊ณผ ๊ฐ™์€ ์ˆซ์žฃ๊ฐ’์„ ๋„ฃ์–ด ๊ธธ์ด๋ฅผ ์ •ํ•ด ์ค˜์•ผ ํ•œ๋‹ค...
// ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค...

 

๋ฐฐ์—ด๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ

- ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์–ป์œผ๋ ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค

String[] weeks = {"์›”", "ํ™”", "์ˆ˜", "๋ชฉ", "๊ธˆ", "ํ† ", "์ผ"}
System.out.println(week[3]); // ๋ชฉ

// weeks[3] => weeks๋ฐฐ์—ด์˜ ๋„ค๋ฒˆ์งธ ํ•ญ๋ชฉ์„ ์˜๋ฏธ(0๋ถ€ํ„ฐ ์ˆซ์ž๋ฅผ ์„ธ์ค€๋‹ค)

 

๋ฐฐ์—ด์˜ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

- ๋ฐฐ์—ด์€ ๋ณดํ†ต for ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ

- ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” length๋ฅผ ์‚ฌ์šฉ

String[] weeks = {"์›”", "ํ™”", "์ˆ˜", "๋ชฉ", "๊ธˆ", "ํ† ", "์ผ"};
// weeks.length = 7
for (int i = 0; i < weeks.length; i++) { 
	System.out.println(weeks[i])
} 
// ์›”
// ํ™”
// ์ˆ˜
// ๋ชฉ
// ๊ธˆ
// ํ† 
// ์ผ

 

๋ฐฐ์—ด ์˜ค๋ฅ˜

- ArrayIndexOutOfBoundsException : ๊ธธ์ด์™€ ์ธ๋ฑ์Šค ์‚ฌ์ด์—์„œ์ƒ๊ธฐ๋Š” ์˜ค๋ฅ˜...

   (ํŒŒ์ด์ฌ์˜ ์ธ๋ฑ์Šค์•„์›ƒ์˜ค๋ธŒ๋ ˆ์ธ์ง€ ๊ฐ™์€ ๋Š๋‚Œ..)

System.out.println(weeks[7]); 
// ์—ฌ๋Ÿ ๋ฒˆ์งธ ๋ฐฐ์—ด๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ArrayIndexOutOfBoundsException ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

7) ๋ฆฌ์ŠคํŠธ

: ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ๋” ๋งŽ์Œ...

๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ์ •ํ•ด์ ธ ์žˆ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ๋ณ€ํ•œ๋‹ค => ์ €์žฅํ•  ๊ฐ’์˜ ํฌ๊ธฐ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค!!

 

 

ArrayList

- ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์—์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ(๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์—๋Š” ArrayList, Vector, LinkedList ๋“ฑ์ด ์žˆ๋‹ค)

 

1. add ๋ฉ”์„œ๋“œ: ์š”์†Ÿ๊ฐ’ ์ถ”๊ฐ€

// ์•ผ๊ตฌ๊ณต์˜ ์‹œ์†์„ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
import java.util.ArrayList;

public class Sample {
	public static void main(String[] args) {
    	ArrayList pitches = new ArrayList();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
    }
}
// import ๋ฌธ : ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋‚˜ ํด๋ž˜์Šค๋ฅผ ํ˜„์žฌ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธํ•˜๋Š” ๋ฌธ์žฅ
// ArrayList๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด import java.util.ArrayList์™€ ๊ฐ™์€ import ๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค
// Java.util ํŒจํ‚ค์ง€๋Š” ์ž๋ฐ”๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€์ด๋‹ค


// ์›ํ•˜๋Š” ์œ„์น˜์— ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด....
pitches.add(0, "133");  -> ์ฒซ๋ฒˆ์งธ ์œ„์น˜์— 133 ์‚ฝ์ž…
pitches.add(1, "133");  -> ๋‘๋ฒˆ์งธ ์œ„์น˜์— 133 ์‚ฝ์ž…

 

2. get ๋ฉ”์„œ๋“œ : ํŠน์ • ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ถ”์ถœ

import java.util.ArrayList;

public class Sample {
	public static void main(String[] args) {
    	ArrayList pitches = new ArrayList();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
        # ์„ ์ˆ˜์˜ ํŠน์ • ํˆฌ๊ตฌ ์†๋„๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด!!!
        System.out.println(pitches.get(1));   // 129
    }
}

 

3. size ๋ฉ”์„œ๋“œ : ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด

System.out.println(pitches.size()); // 3

 

4. contains ๋ฉ”์„œ๋“œ : ๋ฆฌ์ŠคํŠธ ์•ˆ์— ํ•ด๋‹น ํ•ญ๋ชฉ์ด ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•ด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ boolean์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค...

System.out.println(pitches.contains("142")); // true

 

5. remove ๋ฉ”์„œ๋“œ : 2๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋Š”๋ฐ... 1. remove(๊ฐ์ฒด), 2. remove(์ธ๋ฑ์Šค)

// remove(๊ฐ์ฒด)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ์ฒด์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œ , ๊ทธ ๊ฒฐ๊ณผ๋กœ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜
System.out.println(pitches.remove("129")); // true

// remove(์ธ๋ฑ์Šค)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•œ ๋’ค, ๊ทธ ํ•ญ๋ชฉ์„ ๋ฆฌํ„ด
System.out.println(pitches.remove(0)); // 138

 

์ œ๋„ค๋ฆญ์Šค(generics)

: ์ž๋ฃŒํ˜•์„ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ธฐ๋Šฅ(์ž๋ฃŒํ˜•์„ ๊ฐ•์ œ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ์บ์ŠคํŒ… ์˜ค๋ฅ˜๋ฅผ ์ค„์ธ๋‹ค)

 

- ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹ : ArrayList<String> pitches = new ArrayList<String>();

- ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ์‹ : ArrayList<String> pirches = new ArrayList<>();  

=>  <>์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์˜ ์ž๋ฃŒํ˜•์€ ๋ช…ํ™•ํ•˜๋ฏ€๋กœ ๊ตณ์ด ์ ์ง€ ์•Š์•„๋„ ๋œ๋‹ค... (์˜คํžˆ๋ ค ์ ์œผ๋ฉด ์ธํ…”๋ฆฌ์ œ์ด์—์„œ๋Š” ๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€..)

 

- ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด... ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ๋งค๋ฒˆ Object ์ž๋ฃŒํ˜•(๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ƒ์†ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ž๋ฃŒํ˜•)์—์„œ String ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผํ•œ๋‹ค.... 

 

// ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์˜ˆ์‹œ
ArrayList pitches = new ArrayList();
pitches.add("138");
pitches.add("129");

// ํ˜• ๋ณ€ํ™˜์ด ํ•„์š”ํ•จ
String one = (String) pitches.get(0);
String two = (String) pirches.get(1);

// ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ
ArrayList<String> pitches = new ArrayList<>();
pitches.add("138");
pitches.add("129");

// ํ˜•๋ณ€ํ™˜์ด ํ•„์š”์—†์Œ
String one = pitches.get(0)
String two = pitches.get(1)

 

๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ArrayList ๋งŒ๋“ค๊ธฐ

 

1. ArrayList์˜ add ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ : ArrayList ๊ฐ์ฒด์— ์š”์†Œ ์ถ”๊ฐ€

import java.util.AttayList;

public class Sample {
	public staatic void main(String[] args) {
    	ArrayList<String> pitches = new ArrayList<>(); // ์ œ๋„ค๋ฆญ์Šค๋ฅผ ์‚ฌ์šฉํ•œ ํ‘œํ˜„
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
        System.out.println(pitches); // [138, 129, 142]

 

2. ์ด๋ฏธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด ์žˆ๋‹ค๋ฉด...(java.util.Arrays ํด๋ž˜์Šค์˜ asList ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ)

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
    	String[] data = {"138", "129", "142"}; // ์ด๋ฏธ ํˆฌ๊ตฌ ์ˆ˜ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์ด ์žˆ๋‹ค
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList(data));
        System.out.println(pitches);  // [138, 129, 142]
    }
}

 

3. ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋Œ€์‹  ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์„ ์—ฌ๋Ÿฌ๊ฐœ ์ „๋‹ฌํ•˜์—ฌ ์ƒ์„ฑํ•  ์ˆ˜๋„ ....

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        System.out.println(pitches);  // [138, 129, 142]
    }
}

 

String.join("๊ตฌ๋ถ„์ž", ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด)

: ArrayList์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฌด์–ธ๊ฐ€๋กœ(์ฝค๋งˆ(,))๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ 1๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๊ธฐ...

 

1) join์ด ์—†๋‹ค๋ฉด... ์ฝค๋งˆ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ์ฝค๋งˆ๋Š” ์ œ๊ฑฐํ•œ ํ›„ ์ถœ๋ ฅ

2) join์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ• ์ˆ˜ ์žˆ๋‹ค!!

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
    	ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        String result = String.join(",", pirches);
        System.out.println(result); // "138, 129, 142"
    }
}

 

๋ฆฌ์ŠคํŠธ ์ •๋ ฌํ•˜๊ธฐ

: ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ sort ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Sample {
	public static void main(String[] args) {
    	ArrayList<String> pitches = new ArrayList<>(Arrays.asList("138", "129", "142"));
        pitches.sort(Comparator.naturalOrder()); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
        System.out.println(pitches); // [129, 138, 142]
    }
}

- ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ : Comparator.naturalOrder()

- ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ : Comparator.reverseOrder()

 

 


 

8) ๋งต

- ๋”•์…”๋„ˆ๋ฆฌ์™€ ์œ ์‚ฌํ•˜๊ณ   ํ‚ค์™€ ๊ฐ’์„ ํ•œ ์Œ์œผ๋กœ ๊ฐ–๋Š” ์ž๋ฃŒํ˜•

- ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ ์š”์†Ÿ๊ฐ’์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  ํ‚ค๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ์–ป๋Š”๋‹ค

- HashMap, LinkedHashMap, TreeMap ๋“ฑ์ด ์žˆ๋‹ค...

 

HashMap : ๋งต ์ž๋ฃŒํ˜•์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ

1. put ๋ฉ”์„œ๋“œ : key์™€ value๋ฅผ ์ถ”๊ฐ€

import java.util.HashMap;
public class Sample {
	public static void main(String[] args) {
    	HashMap<String, String> map = new HashMap<>();
        map.put("people", "์‚ฌ๋žŒ");
        map.put("baseball", "์•ผ๊ตฌ");
    }
}
// key, value์— String ์ด์™ธ์˜ ์ž๋ฃŒํ˜•์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

 

2. get ๋ฉ”์„œ๋“œ : key์— ํ•ด๋‹นํ•˜๋Š” value๋ฅผ ์–ป์Œ

import java.util.HashMap;
public class Sample {
	public static void main(String[] args) {
    	HashMap<String, String> map = new HashMap<>();
        map.put("people", "์‚ฌ๋žŒ");
        map.put("baseball", "์•ผ๊ตฌ");
        System.out.println(map.get("people")); // ์‚ฌ๋žŒ
    }
}

// ๋งต์˜ key์— ํ•ด๋‹นํ•˜๋Š” value๊ฐ€ ์—†์„ ๋–„ get ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด null์ด ๋ฆฌํ„ด
System.out.println(map.get("java")); // null

// null ๋Œ€์‹  ๊ธฐ๋ณธ๊ฐ’์„ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด getOrDefault ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค
System.out.println(map.getOrDefault("java", "์ž๋ฐ”")); //์ž๋ฐ”

 

3. containsKey ๋ฉ”์„œ๋“œ : ๋งต์— ํ•ด๋‹น key๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ true ๋˜๋Š” false๋กœ ๋ฆฌํ„ด

HashMap<String, String> map = new HashMap<>();
map.put("people", "์‚ฌ๋žŒ");
map.put("baseball", "์•ผ๊ตฌ");
System.out.println(map.containsKey("people")); // true

 

4. remove ๋ฉ”์„œ๋“œ : ๋งต์˜ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ํ•ด๋‹น key์˜ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•œ ํ›„ value ๊ฐ’์„ ๋ฆฌํ„ด

HashMap<String, String> map = new HashMap<>();
map.put("people", "์‚ฌ๋žŒ");
map.put("baseball", "์•ผ๊ตฌ");
System.out.println(map.remove("people")); // ์‚ฌ๋žŒ

 

5. size ๋ฉ”์„œ๋“œ : ๋งต ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด

HashMap<String, String> map = new HashMap<>();
map.put("people", "์‚ฌ๋žŒ");
map.put("baseball", "์•ผ๊ตฌ");
System.out.println(map.containsKey("people")); 
System.out.println(map.size()); // 1

 

6. keySet ๋ฉ”์„œ๋“œ : ๋งต์˜ ๋ชจ๋“  key๋ฅผ ๋ชจ์•„์„œ ๋ฆฌํ„ด

import java.util.HashMap;

public class Sample {
	public static void main(String[] args) {
    	HashMap<String, String> map = new HashMap<>();
        map.put("people", "์‚ฌ๋žŒ");
        map.put("baseball", "์•ผ๊ตฌ");
        System.our.println(map.keySet()); // [baseball, people]
    }
}

 

++ ) ๋งต์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ์ˆœ์„œ์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  key๋กœ value ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด์ง€๋งŒ... ๊ฐ€๋” map์— ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์ž…๋ ฅํ•œ key์— ์˜ํ•ด ์ •๋ ฌํ•˜๋„๋ก ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค...

 

- LinkedHashMap : ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

- TreeMap : ์ž…๋ ฅ๋œ key์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ

 


9) ์ง‘ํ•ฉ

: ์ง‘ํ•ฉ๊ณผ ๊ด€๋ จ๋œ ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ ๊ฒƒ์œผ๋กœ... HashSet, TreeSet, LinkedHashSet ๋“ฑ์ด ์žˆ๋‹ค...

 

- ํŠน์ง• 2๊ฐ€์ง€...

1. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

2, ์ˆœ์„œ๊ฐ€ ์—†๋‹ค(๋ฆฌ์ŠคํŠธ์™€ ๋ฐฐ์—ด์€ ์ˆœ์„œ๊ฐ€ ์žˆ์ฃ ..., ์ˆœ์„œ๊ฐ€ ์—†๋Š” ์ž๋ฃŒํ˜•์ด๋ผ ์ธ๋ฑ์‹ฑ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค)

 

 

๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

1. ์ง‘ํ•ฉ ์ž๋ฃŒํ˜• 2๊ฐœ ๋งŒ๋“ค๊ธฐ

2. ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

3. ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

4. ์ฐจ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

import java.util.Arrays;
import java.util.HashSet;
 
 public class Sample {
 	public static void main(Stirng[] args) {
    	// ์ง‘ํ•ฉ ์ž๋ฃŒํ˜• ๋‘๊ฐœ ๋งŒ๋“ค๊ธฐ 
    	HashSet<Integer> s1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
        HashSet<Integer> s2 = new HashSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));
        
        // (๊ต์ง‘ํ•ฉ) = retainAll ๋ฉ”์„œ๋“œ ์ด์šฉ
        HashSet<Integer> intersection = new HashSet<>(s1); // s1์œผ๋กœ intersection ์ด๋ผ๋Š” HashSet ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•ด ์ƒ์„ฑ
        intersection.retainAll(s2); // ๊ต์ง‘ํ•ฉ ์ˆ˜ํ–‰
        System.out.println(intersection); // [4, 5, 6]
        
        // (ํ•ฉ์ง‘ํ•ฉ) = addAll ๋ฉ”์„œ๋“œ ์ด์šฉ
        HashSet<Integer> union = new HashSet<>(s1); // s1์œผ๋กœ union ์ƒ์„ฑ
        union.addAll(s2); // ํ•ฉ์ง‘ํ•ฉ ์ˆ˜ํ–‰
        System.out.println(union);  // [1, 2, 3, 4, 5, 6, 7, 8, 9]
        
        // (์ฐจ์ง‘ํ•ฉ) = removeAll ๋ฉ”์„œ๋“œ ์ด์šฉ
        HashSet<Integer> substract = new HashSet<>(s1); // s1์œผ๋กœ substract ์ƒ์„ฑ
        substract.removeAll(s2); // ์ฐจ์ง‘ํ•ฉ ์ˆ˜ํ–‰
        System.out.println(substract); // [1, 2, 3]
    }
}

 

์ง‘ํ•ฉ ์ž๋ฃŒํ˜•๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์„œ๋“œ - add, addAll, remove

1. add: ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์— ๊ฐ’์„ ์ถ”๊ฐ€

import java.util.HashSet;

public class Sample {
	public static void main(String[] args) {
    	HashSet<Stirng> set = new HashSet<>();
        set.add("Jump");
        set.add("To");
        set.add("Java");
        System.our.println(set); // [Java, To, Jump]
    }
}

 

2. addAll : ํ•œ๊บผ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ’์„ ์ถ”๊ฐ€

import java.util.HashSet;

public class Sample {
	public static void main(String[] args) {
    	HashSet<Stirng> set = new HashSet<>();
        set.add("Jump");
        set.add("To");
        set.addAll(Arrays.asList("To", "Java"));
        System.our.println(set); // [Java, To, Jump]
    }
}

 

3. remove : ํŠน์ • ๊ฐ’์„ ์ œ๊ฑฐํ•  ๋–„ ์‚ฌ์šฉ

import java.util.HashSet;

public class Sample {
	public static void main(String[] args) {
    	HashSet<Stirng> set = new HashSet<>(Arrays.asList("Jump", "To", "Java"));
        set.add("To");
        System.our.println(set); // [Java, Jump]
    }
}

 


 

10) ์ƒ์ˆ˜ ์ง‘ํ•ฉ

 : enum ์ž๋ฃŒํ˜•์€ ์„œ๋กœ ์—ฐ๊ด€ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ƒ์ˆ˜ ์ง‘ํ•ฉ์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ

public class Sample {
	enum CoffesType {
    	AMERICANO,
        ICE_AMERICANO,
        CAFE_LATTE
    };
    
    public static void main(String[] args) {
    	System.out.println(CoffeeType.AMERICANO);  // AMERICANO
        System.out.println(CoffeeType.ICE_AMERICANO);  // ICE_AMERICANO
        System.out.println(CoffeeType.CAGE_LATTE);   // CAFE_LATTE
    }
}


// ๋ฐ˜๋ณต๋ฌธ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
public class Sample {
	enum CoffesType {
    	AMERICANO,
        ICE_AMERICANO,
        CAFE_LATTE
    };
    
     public static void main(String[] args) {
     	for(CoffeeType type: CoffeeType.values()) {
        	System.out.println(type);  // ์ˆœ์„œ๋Œ€๋กœ AMERICANO, ICE_AMERICANO, CAFE_LATTE ์ถœ๋ ฅ
        }
    }
}

- enum ์‚ฌ์šฉ์˜ ์žฅ์ 

1. ๋งค์ง ๋„˜๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ์ฝ”๋“œ๊ฐ€ ๋ช…ํ™•ํ•˜๋‹ค

2. ์ž˜๋ชป๋œ ๊ฐ’์„ ์‚ฌ์šฉํ•ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค

 

 


11) ํ˜• ๋ณ€ํ™˜๊ณผ final

: ํ˜• ๋ณ€ํ™˜์ด๋ž€ ์ž๋ฃŒํ˜•์„ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ ("123"๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ 123๊ณผ ๊ฐ™์€ ์ˆซ์žํ˜•์œผ๋กœ)

 

ํ˜•๋ณ€ํ™˜ 

1.  ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ

String num = "123";
//๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„œ๋Š” Integer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค

public class Sample {
	public static void main(String[] args) {
    	String num = "123";
        int n = Integer.parseInt(num);
        System.out.println(n); 			// 123
    }
}

 

2. ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ : ์ •์ˆ˜ ์•ž์— ๋นˆ ๋ฌธ์ž์—ด์„ ๋”ํ•ด ์ฃผ๊ธฐ 

public class Sample {
	public static void main(String[] args) {
    	int n = 123;
        String num = "" + n;
        System.out.println(num);  // 123
    }
}

 

 ++)  String.valueOf(์ •์ˆ˜), Integer.toString(์ •์ˆ˜) ๋ชจ๋‘ ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ๋‹ค....

public class Sample {
	public static void main(String[] args) {
    	int n = 123;
        String num1 = String.valueOf(n);
        String num2 = Integer.toString(n);
        System.out.println(num1);   // 123
        System.out.println(num2);   // 123
    }
}

 

3. ์†Œ์ˆ˜์ ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ˆซ์ž ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์˜ ๋ณ€ํ™˜

public class Sample {
	public static void main(String[] args) {
    	String num = "123.456";
        double d = Double.parseDouble(num);
        System.out.println(d);    // 123.456
    }
}

 

4. ์ •์ˆ˜์™€ ์‹ค์ˆ˜ ์‚ฌ์ด์˜ ํ˜• ๋ณ€ํ™˜

public class Sample {
	public static void main(String[] args) {
    	int n1 = 123;
        double d1 = n1;
        System.out.println(d1);  // 123.0
        
        double d2 = 123.456;
        int n2 = (int) d2;
        System.out.println(n2);  // 123
    }
}

 

5. ์‹ค์ˆ˜ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊ฟ€ ๋•Œ

public class Sample {
	public static void main(String[] args) {
    	String num = "123.456";
        int n = Integer.parseInt(num);  // ์‹ค์ˆ˜ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ๊ฒฝ์šฐ numberformatException์ด ๋ฐœ์ƒ
    }
}

// ์‹ค์ˆ˜ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์„ ์ˆซ์žํ˜•์œผ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด... Double.parseDouble()์„ ์ด์šฉํ•ด ์‹ค์ˆ˜๋กœ ๋ณ€ํ™˜ ํ›„ ์‚ฌ์šฉ

 

final

: ์ž๋ฃŒํ˜•์—์„œ ๊ฐ’์„ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๊ฐ•์ œํ•˜๋Š” ํ‚ค์›Œ๋“œ (๋‹ค์‹œ ์„ค์ • ์•ˆ๋ฉ๋‹ˆ๋‹ค)

public class Sample {
	public static void main(String[] args) {
    	final int n = 123;   // final๋กœ ์„ค์ •ํ•˜๋ฉด ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค
        n = 456;
    }
}

 

๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ๋„ final๋กœ ์„ ์–ธํ•˜๋ฉด ์žฌํ• ๋‹น์€ ๋ถˆ๊ฐ€๋Šฅ...(๋”ํ•˜๊ฑฐ๋‚˜ ๋บด๊ธฐ๋Š” ๊ฐ€๋Šฅ)

import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
	public static void main(String[] args) {
    	final ArrayList<String> a = new ArrayList<>(Arrays.asList("a", "b"));
        a = new ArrayList<>(Arrays,asList("c", "d")); // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
    }
}

// ๊ทธ๋Ÿฌ๋ฏ€๋กœ final์€ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๊ทธ ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด ์•ˆ ๋  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค

 

๋ฆฌ์ŠคํŠธ์˜ ์žฌํ• ๋‹น ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๋Š” ๊ฒƒ๋„ ํ•  ์ˆ˜ ์—†๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด... List.of๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค!!!

import java.util.List;

public class Sample {
	public static void main(String[] args) {
    	final List<String> a = List.of("a", "b");
        a.add("c"); // UnsupportedOperationException ๋ฐœ์ƒ
    }
}

 

++) ๋ฌธ์ œ ํ’€์ด

 

6. myList.sort(Comparator,reverseOrder());

7. String result = String.join(" ", myList);

8. int result = grade.remove("B")

9. ArrayList<Integer> result = new ArratList<>(temp);

10. 
public class Sample{
	enum CoffeeType {
    	AMERICANO,
        ICE_AMERICANO,
        CAFE_LATTE
    };
    
 ...
 
 printCoffeePrice(CoffeeType,AMERICANO);

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

๋“œ๋””์–ด ๋ฐฐ์—ด๋กœ ๋„˜์–ด๊ฐ”์Šต๋‹ˆ๋‹ค^^

ํŒŒ์ด์ฌ๋ฐ˜์— ๋“ค์–ด๊ฐ€๋ฉด์„œ ํŒŒ์ด์ฌ์— ๋Œ€ํ•ด ์—ด์‹ฌํžˆ ๋ฐฐ์šฐ๊ณ  ์žˆ๋Š”๋ฐ์š”...

์ž๋ฐ”๋ฅผ ๋†“์•„๋ฒ„๋ฆฌ๋ฉด ์•ˆ๋ ๊ฑฐ ๊ฐ™์•„์„œ ๊พธ์ค€ํžˆ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช

 

์ด๋ฒˆ์ฃผ ๋ชฉํ‘œ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””(ํŒŒ์ด์ฌ) ์—ด์‹ฌํžˆ ํ•˜๊ธฐ !  , ๋ฐฑ์ค€ ์ž๋ฐ” 1์ฐจ์› ๋ฐฐ์—ด ๋๋‚ด๊ธฐ์ž…๋‹ˆ๋‹ค !!

 

๋ฌธ์ œ

์ด N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜ v๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ •์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ์žˆ๋‹ค. ์…‹์งธ ์ค„์—๋Š” ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋Š” ์ •์ˆ˜ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์™€ v๋Š” -100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ N๊ฐœ์˜ ์ •์ˆ˜ ์ค‘์— v๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

 

import java.util.Scanner;

public class Solution_10807 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		# ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ N ์ž…๋ ฅ๋ฐ›๊ธฐ
        int N = sc.nextInt();
		
        # N๊ฐœ์˜ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
		int arr[] = new int[N];
		for(int i = 0; i < N; i++) {
			arr[i] = sc.nextInt();
		}
        
        # ๊ฐ™์€ ์ˆ˜๋กœ ์ฐพ์„ v ์ž…๋ ฅ๋ฐ›๊ธฐ
		int v = sc.nextInt();
		sc.close();
		
        # ๋ฐฐ์—ด์ค‘์—์„œ v์™€ ๊ฐ™์€ ์ˆ˜ ์นด์šดํŠธ
		int count = 0;
		for(int i = 0; i < N; i++) {
			if(v == arr[i])
				count++;
		}
		System.out.println(count);
	}
}

 

ํŒŒ์ด์ฌ ๊ณ„์†ํ•˜๋‹ค๋ณด๋‹ˆ ์ž๋ฐ” ๋ฐฐ์—ด ๊ฐœ๋…์ด ๊ธฐ์–ต์ด ์•ˆ๋‚˜์„œ ์กฐ๊ธˆ ๋‹ค์‹œ ๋ณด๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ int arr[] = new int[N] ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ์ด์ง€ ์•Š์•˜์„๊นŒ..

 

 

import java.util.Arrays;๋ฅผ ์™œ ์•ˆ ์“ฐ์ง€ ์ฐพ์•„๋ดค๋Š”๋ฐ ๋ฐฐ์—ด ์ •๋ ฌ, ๋น„๊ต, ์ฑ„์šฐ๊ธฐ ํ•  ๋–„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋”๋ผ๊ตฌ์—ฌ,,,

์ผ๋ฐ˜์ ์ธ ์ƒ์„ฑ๋งŒ์„ ํ•  ๋•Œ๋Š” ์•„๋งˆ๋„ ์•ˆ ์ ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค(?)

 

https://chb2005.tistory.com/6

 

[JAVA] ๋ฐฐ์—ด + java.util.Arrays ํŒจํ‚ค์ง€ ํ™œ์šฉ

๋ฐฐ์—ด ๊ธฐ๋ณธ ๋ฌธ๋ฒ• 1์ฐจ์› ๋ฐฐ์—ด ์ƒ์„ฑ (int ํ˜•) int[] array = new int[5]; // ๋ฐฉ๋ฒ• 1 int array[] = new int[5]; // ๋ฐฉ๋ฒ• 2 5์นธ์งœ๋ฆฌ intํ˜• ๋ฐฐ์—ด ์ƒ์„ฑ (index : 0~4) ์ดˆ๊ธฐ๊ฐ’์„ ๋”ฐ๋กœ ์ง€์ • ์•ˆํ•ด์ฃผ๋ฉด 0์ด ๋“ค์–ด๊ฐ (String์˜ ๊ฒฝ์šฐ null๊ฐ’

chb2005.tistory.com

์š” ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ•ด์„œ ๋ฌธ์ œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜ฎ๐Ÿ‘๐Ÿ™Œ

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

< 10951. A+B - 4 > 

์ž…๋ ฅ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ. EOF์— ๋Œ€ํ•ด ์•Œ์•„ ๋ณด์„ธ์š”!!

 

๋ฌธ์ œ

๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10)

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์•„๋ฌด์ƒ๊ฐ ์—†์ด ์•ž๋ฌธ์ œ๋ž‘ ๋˜‘๊ฐ™๋„ค ๋ญ์•ผ,,, ํ•˜๋ฉด์„œ ์—ด์‹ฌํžˆ ์ ์—ˆ๋Š”๋ฐ ๊ณ„์† ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”....ใ… 

๊ทธ๋ž˜์„œ ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ดค๋Š”๋ฐ ๋ฌธ์ œ ๋ฐ‘์— EOF์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ผ๊ณ  ํ•˜๋”๋ผ๊ตฌ์—ฌ ;;;;

EOF๋Š” ๋˜ ๋ญ๋žŒ...

 


 

EOF ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿค”๐Ÿค”

 

EOF : ๋ฐ์ดํ„ฐ ์†Œ์Šค๋กœ๋ถ€ํ„ฐ ๋” ์ด์ƒ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์šฉ์–ด

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์ž…๋ ฅ๊ฐ’์„ ์–ผ๋งˆ๋‚˜ ๋ฐ›์„์ง€ ๋ช…์‹œํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!!

 

1) Scanner ์‚ฌ์šฉ ์‹œ

 

  • hasNext() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
  • ์ž…๋ ฅ๋œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

 

2) BufferReader ์‚ฌ์šฉ์‹œ

 

  • null ์ด ์•„๋‹˜์„ ๋ช…์‹œํ•ด ์ค๋‹ˆ๋‹ค!!

 

ํŒŒ์ด์ฌ๊ณผ C++ ์–ธ์–ด ์‚ฌ์šฉ์˜ ๊ฒฝ์šฐ์— ๋‹ค ๋‹ค๋ฅด๋ฏ€๋กœ ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด ๋ณ„ EOF๋ฅผ ๊ฒ€์ƒ‰ํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ˜‚

 


import java.util.Scanner;
 
public class Main {
	public static void main(String args[]){
		
		Scanner in=new Scanner(System.in);
			
		while(in.hasNextInt()){
		
			int a=in.nextInt();
			int b=in.nextInt();
			System.out.println(a+b);
		}	
		in.close();
	}
}

 

์›๋ž˜๋ผ๋ฉด while(true)๋ฅผ ์จ์ค„๊ป€๋ฐ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ ์•Œ ์ˆ˜ ์—†์œผ๋‹ˆ๊นŒ while(in.hasNextInt())๋ฅผ ์‚ฌ์šฉํ•ด์คฌ๋‹ค

์ง„์งœ ๋ช‡๋ฒˆ ํ‹€๋ ธ๋Š”์ง€ ์…€ ์ˆ˜๋Š” ์žˆ์ง€๋งŒใ…‹ ๋งŽ์ด ํ‹€๋ ธ๋‹ค..

 

 

์ด๊ฑฐ BufferReader๋กœ ๋ฐ”๊พธ๋ฉด ๋˜ ๋งŽ์ด ํ‹€๋ฆด๊ฒƒ ๊ฐ™์•˜์ง€๋งŒ ํ•ด๋ด์•ผํ•  ๊ฑฐ ๊ฐ™์•„์„œ ์ผ๋‹จ ํ•ด๋ดค๋‹ค.

import java.io.*;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;
		
		while (true) {
			String str = br.readLine();
			if (str == null) {
				break;
				
			}
			
			st = new StringTokenizer(str);
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			
			bw.write(a + b + "\n");
		}
		bw.close();
	}
}

 

๊ฐ’์ด ์—†์œผ๋ฉด ๋ฉˆ์ถฐ์•ผ ํ•˜๋‹ˆ๊นŒ str == null์„ ์‚ฌ์šฉํ•ด์คฌ๋‹ค.

 

๋ฉ”๋ชจ๋ฆฌ๋‚˜ ์‹œ๊ฐ„์ ์œผ๋กœ๋‚˜ ์ ๊ฒŒ ๊ฑธ๋ ค์„œ ๋ฒ„ํผ๋ฆฌ๋” ์“ฐ๋Š” ์—ฐ์Šต์„ ์—ด์‹ฌํžˆ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค.. ์‹ค์ œ๋กœ ์ž๋ฐ”๊ฐ€ ๋‹ค๋ฅธ ์–ธ์–ด๋ณด๋‹ค ๋Š๋ฆฌ๋‹ค๊ณ  ํ•˜๋‹ˆ ๋”๋”์šฑ ๋ฒ„ํผ ์—ฐ์Šตํ•ด์•ผํ•  ๋“ฏ

 

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

<10952. A+B - 5>

๋ฌธ์ œ

๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10)

์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์—๋Š” 0 ๋‘ ๊ฐœ๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

package bj0715;

import java.util.Scanner;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
	
		while (true) {
			int a = sc.nextInt();			
			int b = sc.nextInt();
			
			if (a == 0 && b == 0) {
				sc.close();
				break;
			}
			
			else  {
				System.out.println(a + b);
			}
		}
	}
}

์ฒ˜์Œ์— ๋ฌธ์ œ๋„ ์ œ๋Œ€๋กœ ์•ˆ ์ผ๊ณ  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ๊ณ„์† ์ •๋‹ต์ด ์•ˆ ๋‚˜์™€์„œ ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์ž…๋ ฅํ•œ๋‹ค๋Š” ์กฐ๊ฑด์ด ์—†์—ˆ๋‹ค ;;;;;

์ด๋ž˜์„œ ๋ฌธ์ œ๋ฅผ ์ž˜ ์ฝ์–ด์•ผ ํ•œ๋‹ค๊ณ ...

 

 

์•„๋ฌด๋ฆฌ ์ƒ๊ฐํ•ด๋„ for ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์•ˆํ’€๋ฆฌ๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋ฐ˜๋ณต๋ฌธ์— for๋งŒ ์žˆ์—ˆ๋˜๊ฒŒ ์•„๋‹ˆ์—ˆ๋˜๊ฑฐ ๊ฐ™๋‹ค^^ ์‹ถ์—ˆ์Œ

๊ทธ๋ž˜์„œ while๋ฌธ์„ ์‚ฌ์šฉํ•ด์คฌ๊ตฌ์—ฌ... ๋ฌธ์ œ๋Š”  a, b ๋‘˜๋‹ค 0์ผ๋•Œ๋งŒ while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ค ๐Ÿ˜‚

 

 

for๋ฌธ, while๋ฌธ์€ ์„œ๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•  ๋•Œ ์–ด๋Š ์ชฝ์„ ์„ ํƒํ•ด๋„ ์ข‹์ง€๋งŒ, for๋ฌธ์€ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ์„ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  while๋ฌธ์€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋ณตํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!!

 

 

๊ทผ๋ฐ sc.close๊ฐ€ ์ž…๋ ฅ์„ ๋‹ค ๋ฐ›๊ณ  ๋‚œ ํ›„๋กœ๋Š” ์–ด๋””๋“  ์จ๋„ ๋œ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ๋Š”๋ฐ ํ”„๋ฆฐํŠธ ์•ž์ค„์— ์ผ๋Š”๋ฐ ์•ˆ๋˜๋”๋ผ๊ตฌ์—ฌ..

ํ˜น์‰ฌ ์•„์‹œ๋Š” ๋ถ„ ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‚ผ์œ  โœ๏ธโœ๏ธ

 

 

 

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

2438. ๋ณ„ ์ฐ๊ธฐ - 1

๋ฌธ์ œ

์ฒซ์งธ ์ค„์—๋Š” ๋ณ„ 1๊ฐœ, ๋‘˜์งธ ์ค„์—๋Š” ๋ณ„ 2๊ฐœ, N๋ฒˆ์งธ ์ค„์—๋Š” ๋ณ„ N๊ฐœ๋ฅผ ์ฐ๋Š” ๋ฌธ์ œ

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ๋ณ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

- Scanner๋ฌธ ์‚ฌ์šฉ 

import java.util.Scanner;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
        sc.close();
		
		
		for (int i =1; i<=N; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
		System.out.println();
		} 
	}
}

 

N๊ฐœ์˜ ์ค„์— N๊ฐœ์˜ ๋ณ„์ด ๋‚˜์™€์•ผ ํ•˜๋ฏ€๋กœ ์ด์ค‘ ํฌ๋ฌธ์„ ๋Œ๋ ค์ฃผ๊ณ  ๋„์›Œ์ค˜์•ผํ•˜๋‹ˆ๊นŒ

N๊ฐœ์˜ ์ค„์— ํ•ด๋‹นํ•˜๋Š” ํฌ๋ฌธ์œผ๋กœ ๋‚˜์™€์„œ println()์„ ๋„ฃ์–ด์คฌ์Šต๋‹ˆ๋‹ค~~~

๋ณ„์ฐ๊ธฐ --2 ๋ฒˆ์€ BufferedReader/BufferWriter ์จ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

 

 


2439. ๋ณ„ ์ฐ๊ธฐ - 2

๋ฌธ์ œ

์ฒซ์งธ ์ค„์—๋Š” ๋ณ„ 1๊ฐœ, ๋‘˜์งธ ์ค„์—๋Š” ๋ณ„ 2๊ฐœ, N๋ฒˆ์งธ ์ค„์—๋Š” ๋ณ„ N๊ฐœ๋ฅผ ์ฐ๋Š” ๋ฌธ์ œ

ํ•˜์ง€๋งŒ, ์˜ค๋ฅธ์ชฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋ณ„(์˜ˆ์ œ ์ฐธ๊ณ )์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ๋ณ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

import java.io.*;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;
		int N = Integer.parseInt(br.readLine());
		br.close();
		
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N - i; j++) {
				bw.write(' ');
			}
			for (int k = 1; k <= i; k++) {
				bw.write('*');
			}
			bw.newLine();
		}
		bw.flush();
		bw.close();
	}
}

 

์ €๋ฒˆ์— import java.io.*;์„ ์‚ฌ์šฉ์„ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ ์•Œ๊ณ  ๋ณด๋‹ˆ๊นŒ *์ „์— ๋งˆ์ง€๋ง‰ .์„ ์•ˆ ์ฐ์—ˆ๋”๋ผ๊ตฌ์š”...

์ง„์งœ import ๋„ˆ๋ฌด ๋งŽ์•„๊ฐ€์ง€๊ณ  ๋‹ค ์ ๊ธฐ ๊ท€์ฐฎ์•„์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ์‹œ๋„ํ•ด๋ดค๋Š”๋ฐ ๋นจ๊ฐ„์ค„ ์•ˆ ๋– ์„œ ๋จธ์“ฑํ–ˆ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

 

๊ณต๋ฐฑ ์ถœ๋ ฅ๊นŒ์ง€๋Š” ์‰ฌ์› ๋Š”๋ฐ ๋‹ค์Œ์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผ์ด์—ˆ๋Š”๋ฐ ๋ช‡๋ฒˆ์˜ ์ด์ƒํ•œ ๋ณ„ ์ฐ๊ธฐ ๋์—

๋‹ค์‹œ ํ•œ๋ฒˆ for๋ฌธ์„ ๋Œ๋ฆฌ๋‹ˆ๊นŒ ์™„์„ฑ๋˜์—ˆ๋‹ค~~~

 

๋“œ๋””์–ด ๋ฐ˜๋ณต๋ฌธ ๋‘๋ฌธ์ œ ๋‚จ์•˜๋„ค์—ฌ... ๋ฐฐ์—ด๋„ ํ™”์ดํŒ…๐Ÿ’ช๐Ÿ’ช๐Ÿ’ช

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts