diff --git "a/\354\244\200\355\231\230/chapter06.md" "b/\354\244\200\355\231\230/chapter06.md" new file mode 100644 index 0000000..d4e0dbb --- /dev/null +++ "b/\354\244\200\355\231\230/chapter06.md" @@ -0,0 +1,54 @@ + +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. this ์˜ˆ์•ฝ์–ด +2. ๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ +3. static ๋ณ€์ˆ˜ +4. ๋ณ€์ˆ˜ ์œ ํšจ ๋ฒ”์œ„ +5. static ์‘์šฉ - ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด +### ๐Ÿง  ๋ฐฐ์šด ์  +- this : ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค ์Šค์Šค๋กœ๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” ์˜ˆ์•ฝ์–ด +- this ์ถœ๋ ฅํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/584c5b29-db10-4e5e-bfd1-08b030993deb) +- ![image](https://github.com/user-attachments/assets/e6935719-46ae-4f38-8bda-71060e4259ca) +- this๋กœ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ˜ธ์ถœํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/26e36732-baeb-4fd2-ba49-989195294dd3) +- ![image](https://github.com/user-attachments/assets/66445622-8787-4ffe-8092-fc2c009cbcb0) +- this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์†Œ ๊ฐ’ ๋ฐ˜ํ™˜ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/0beee8b9-7072-4f59-b1e6-5ebdfae6936a) +- ![image](https://github.com/user-attachments/assets/7693e78d-94e6-43d7-9bd4-6fab44504c9a) +- ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์€ ๊ฐ์ฒด๋ฅผ ์ •์˜ ํ•˜๊ณ  ๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ์œผ๋กœ ๋งŒ๋“ค์–ด ์ง„๋‹ค๊ณ  ํ–ˆ๋‹ค +- ex) ![image](https://github.com/user-attachments/assets/fd09f943-adfa-42ba-a580-0e9a289d6325) +- ํ•™์ƒ ํด๋ž˜์Šค ๊ตฌํ˜„ +- ![image](https://github.com/user-attachments/assets/67e5a1d2-d781-49c2-a762-b940b5a04e47) +- ๋ฒ„์Šค ํด๋ž˜์Šค ๊ตฌํ˜„ +- ![image](https://github.com/user-attachments/assets/5885083f-01a4-442f-b07d-5510c29c6d52) +- ์ง€ํ•˜์ฒ  ํด๋ž˜์Šค ๊ตฌํ˜„ +- ![image](https://github.com/user-attachments/assets/79692f53-e370-4d2b-9b73-56e12388eeae) +- ๋ฒ„์Šค์™€ ์ง€ํ•˜์ฒ  ํƒ€๊ธฐ +- ![image](https://github.com/user-attachments/assets/41fa3b5f-2840-4c94-9498-e1ae50857096) +- ![image](https://github.com/user-attachments/assets/025d1611-e1d7-459c-8243-ca52b20567ad) +- static ๋ณ€์ˆ˜ : ํด๋ž˜์Šค์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜ +- static ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/b6610d1e-f807-43df-a922-cf5ab879bfe3) +- ๋ณ€์ˆ˜ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/2c751c62-9b7b-43ba-b1a1-ef9ec7b33425) +- ![image](https://github.com/user-attachments/assets/f13bfa10-1c1e-415e-8bc3-90e5d6410646) +- ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ static ๋ณ€์ˆ˜ ์ฐธ์กฐํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/af656a11-a19b-4334-89b4-e9c3d203a792) +- ![image](https://github.com/user-attachments/assets/da71ff0b-1276-4647-a39b-20bb40bf091b) +- seriaNum์˜ get(),set() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/d902ab83-62e9-4e87-b728-32a365143b38) +- ๋ณ€์ˆ˜ ์œ ํšจ ๋ฒ”์œ„ +- ![image](https://github.com/user-attachments/assets/803c0a3b-4fb4-47bc-aba6-d517ef697a94) +- ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด +- ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด +- ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ํšŒ์‚ฌ ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +- 1๋‹จ๊ณ„ ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ๋งŒ๋“ค๊ธฐ +- ![image](https://github.com/user-attachments/assets/96b0e174-d6bc-4aa4-9ae5-a42a353dc6c6) +- 2๋‹จ๊ณ„ static์œผ๋กœ ์œ ์ผ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ +- ![image](https://github.com/user-attachments/assets/e9c4f9b8-7278-4997-b299-dcd0cbcc716d) +- 3๋‹จ๊ณ„ ์™ธ๋ถ€์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” public ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ +- ![image](https://github.com/user-attachments/assets/f908eb3e-7e63-47c0-ba6f-22b45b286381) +- 4๋‹จ๊ณ„ ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ ๋งŒ๋“ค๊ธฐ +- ![image](https://github.com/user-attachments/assets/cd0dd064-f765-4a97-ab21-e3fea554f62a) + diff --git "a/\354\244\200\355\231\230/chapter07.md" "b/\354\244\200\355\231\230/chapter07.md" new file mode 100644 index 0000000..0acb7c8 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter07.md" @@ -0,0 +1,24 @@ + +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก + 1. ๋ฐฐ์—ด์ด๋ž€ + 2. ๋‹ค์ฐจ์› ๋ฐฐ์—ด + 3. ArrayList ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ + 4. ๋ฐฐ์—ด ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ +### ๐Ÿง  ๋ฐฐ์šด ์  +- ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ํ•˜๊ณ  ์ถœ๋ ฅํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/7f461faf-aadc-40af-bd6f-28bd936f1ba9) +- ![image](https://github.com/user-attachments/assets/df80d09f-e43e-41cc-8abe-39b6afbad0cc) +- ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ์ถœ๋ ฅํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/c51a594e-abcd-4b83-9362-88926a071dcf) +- ๋ฐฐ์—ด์˜ ์œ ํšจํ•œ ์š”์†Œ ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/69e68b8a-86dd-4559-b675-37326ff351cf) +- ๋ฌธ์ž ์ €์žฅ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ +- ![image](https://github.com/user-attachments/assets/0a26aa89-b937-4df0-a5d2-a89817d2e24d) +- ๊ฐ์ฒด ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/5fca6865-4030-466a-9199-acc974547fe0) +- ๊ฐ ๋ฐฐ์—ด ์š”์†Œ์— ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ๋„ฃ๊ธฐ +- ![image](https://github.com/user-attachments/assets/f8ed8189-559a-420a-8133-7973eb1a237d) +- ์ด์ฐจ์› ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/ae6627f7-7e34-4d93-8cd4-f05d5bc40758) +- ArraryList ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/b733ce6f-d6cb-462e-9c0a-a3ede3bfb1b0) diff --git "a/\354\244\200\355\231\230/chapter08.md" "b/\354\244\200\355\231\230/chapter08.md" new file mode 100644 index 0000000..4a386dc --- /dev/null +++ "b/\354\244\200\355\231\230/chapter08.md" @@ -0,0 +1,144 @@ + + +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ์ƒ์†์ด๋ž€ +2. ์ƒ์†์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ๊ณผ ํ˜• ๋ณ€ํ™˜ +3. ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ +4. ๋‹คํ˜•์„ฑ +5. ๋‹คํ˜•์„ฑ ํ™œ์šฉ +6. ๋‹ค์šด ์บ์ŠคํŒ…๊ณผ instanceof +### ๐Ÿง  ๋ฐฐ์šด ์  +- ์ƒ์†์ด๋ž€ +- Bํด๋ž˜์Šค๊ฐ€ Aํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•˜์„๋•Œ Bํด๋ž˜์Šค๋Š” Aํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. +- Customer ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/7c266bc1-c9d9-42dd-9be4-d3f43d4b90c2) +- vip ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/b54b24a0-e7c6-4d6a-8f39-15d0b3099968) +customer ์ฝ”๋“œ์—์„œ ๋ญ”๊ฐ€ ์ž˜๋ชปํ•ด์„œ ๊ทธ ๋ถ€๋ถ„๋งŒ ๋‚˜์ค‘์— ๋‹ค์‹œ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. +- ๋‹คํ˜•์„ฑ์ด๋ž€ +- ํ•˜๋‚˜์˜ ์ฝ”๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ์ž๋ฃŒํ˜•์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. +- ๋‹คํ–ฅ์„ฑ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ +``` +package chapter08.polymorphism; + +class Animal { + public void move() { + System.out.println("๋™๋ฌผ์ด ์›€์ง์ž…๋‹ˆ๋‹ค"); + } +} + +class Human extends Animal { + public void move() { + System.out.println("์‚ฌ๋žŒ์ด ๋‘ ๋ฐœ๋กœ ๊ฑท์Šต๋‹ˆ๋‹ค"); + } +} + +class Tiger extends Animal { + public void move() { + System.out.println("ํ˜ธ๋ž‘์ด๊ฐ€ ๋„ค ๋ฐœ๋กœ ๋œ๋‹ˆ๋‹ค."); + } +} + +class Eagle extends Animal { + public void move() { + System.out.println("๋…์ˆ˜๋ฆฌ๊ฐ€ ํ•˜๋Š˜์„ ๋‚ฉ๋‹ˆ๋‹ค"); + } +} + +public class AnimalTest1 { + public static void main(String[] args) { + AnimalTest1 aTest = new AnimalTest1(); // ์ž๊ธฐ ์ž์‹  ๊ฐ์ฒด ์ƒ์„ฑ + aTest.moveAnimal(new Human()); + aTest.moveAnimal(new Tiger()); + aTest.moveAnimal(new Eagle()); + } + + public void moveAnimal(Animal animal) { + animal.move(); + } +} + +``` +![image](https://github.com/user-attachments/assets/809f2846-37de-44ce-9898-dce2da34f779) +- instanceof๋กœ ์›๋ž˜ ์ธ์Šคํ„ด์Šคํ˜• ํ™•์ธ ํ›„ ๋‹ค์šด ์บ์ŠคํŒ… ํ•˜๊ธฐ +''' +package chapter08.polymorphism; +import java.util.ArrayList; + +class Animal { + public void move() { + System.out.println("๋™๋ฌผ์ด ์›€์ง์ž…๋‹ˆ๋‹ค."); + } +} + +class Human extends Animal { + public void move() { + System.out.println("์‚ฌ๋žŒ์ด ๋‘ ๋ฐœ๋กœ ๊ฑท์Šต๋‹ˆ๋‹ค."); + } + + public void readBook() { + System.out.println("์‚ฌ๋žŒ์ด ์ฑ…์„ ์ฝ์Šต๋‹ˆ๋‹ค."); + } +} + +class Tiger extends Animal { + public void move() { + System.out.println("ํ˜ธ๋ž‘์ด๊ฐ€ ๋„ค ๋ฐœ๋กœ ๋œ๋‹ˆ๋‹ค."); + } + + public void hunting() { + System.out.println("ํ˜ธ๋ž‘์ด๊ฐ€ ์‚ฌ๋ƒฅ์„ ํ•ฉ๋‹ˆ๋‹ค."); + } +} + +class Eagle extends Animal { + public void move() { + System.out.println("๋…์ˆ˜๋ฆฌ๊ฐ€ ํ•˜๋Š˜์„ ๋‚ฉ๋‹ˆ๋‹ค."); + } + + public void flying() { + System.out.println("๋…์ˆ˜๋ฆฌ๊ฐ€ ๋‚ ๊ฐœ๋ฅผ ์ญ‰ ํŽด๊ณ  ๋ฉ€๋ฆฌ ๋‚ ์•„๊ฐ‘๋‹ˆ๋‹ค."); + } +} +public class AnimalTest { + ArrayList aniList = new ArrayList(); // Animal ํƒ€์ž…์˜ ArrayList ์„ ์–ธ + + public static void main(String[] args) { + AnimalTest aTest = new AnimalTest(); + System.out.println("์›๋ž˜ ์œต์œผ๋กœ ๋‹ค๋ฃฐ ์บ์ŠคํŒ…"); + aTest.addAnimal(); + aTest.testCasting(); + } + + public void addAnimal() { + aniList.add(new Human()); // Human ๊ฐ์ฒด ์ถ”๊ฐ€ + aniList.add(new Tiger()); // Tiger ๊ฐ์ฒด ์ถ”๊ฐ€ + aniList.add(new Eagle()); // Eagle ๊ฐ์ฒด ์ถ”๊ฐ€ + + for (Animal ani : aniList) { + ani.move(); // ๊ฐ ๋™๋ฌผ์˜ move ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ + } + } + + public void testCasting() { + for (int i = 0; i < aniList.size(); i++) { // ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋Œ๋ฉด์„œ + Animal ani = aniList.get(i); // Animal ํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜ด + if (ani instanceof Human) { + Human h = (Human) ani; // Human์ด๋ฉด ์บ์ŠคํŒ… + h.readBook(); // Human์˜ readBook ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ + } else if (ani instanceof Tiger) { + Tiger t = (Tiger) ani; // Tiger์ด๋ฉด ์บ์ŠคํŒ… + t.hunting(); // Tiger์˜ hunting ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ + } else if (ani instanceof Eagle) { + Eagle e = (Eagle) ani; // Eagle์ด๋ฉด ์บ์ŠคํŒ… + e.flying(); // Eagle์˜ flying ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ + } else { + System.out.println("์ง€์›๋˜์ง€ ์•Š๋Š” ํ˜•์ž…๋‹ˆ๋‹ค."); // ์ง€์›๋˜์ง€ ์•Š๋Š” ํ˜•์ผ ๊ฒฝ์šฐ + } + } + } +} + +''' +- ![image](https://github.com/user-attachments/assets/889a55fc-6336-48d1-a8a8-be06c785e2b3) +- diff --git "a/\354\244\200\355\231\230/chapter09.md" "b/\354\244\200\355\231\230/chapter09.md" new file mode 100644 index 0000000..71e2183 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter09.md" @@ -0,0 +1,47 @@ + + +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ์ถ”์ƒ ํด๋ž˜์Šค +2. ํƒฌํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ +3. ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ์‘์šฉํ•˜๊ธฐ +4. final ์˜ˆ์•ฝ์–ด +### ๐Ÿง  ๋ฐฐ์šด ์  +- ์ถ”์ƒ ํด๋ž˜์Šค๋ž€? +- ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋Š” ๊ตฌํ˜„ ์ฝ”๋“œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ ๊ตฌํ˜„ ์ฝ”๋“œ๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜ ๋ชธ์ฒด๊ฐ€ ์—†๋‹ค๋Š” ๋œป +![image](https://github.com/user-attachments/assets/f8c82850-a8ff-4e6f-883c-af3c03d74355) +์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ผ ๋ถ€๋ถ„์€ ํ•จ์ˆ˜์˜ ๊ตฌํ˜„๋ถ€๋ผ๊ณ  ํ•œ๋‹ค. +์ด ๋ถ€๋ถ„์ด ์—†๋Š” ํ•จ์ˆ˜๋Š” ์ถ”์ƒ ํ•จ์ˆ˜์ด๊ณ  ์ž๋ฐ”์—์„œ๋Š” ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. +์ถ”์ƒ๋ฉ”์„œ๋“œ๋Š” ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์„ ์–ธ๋งŒ ํ•˜๋ฉฐ abstract ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  {} ๋Œ€์‹ ์— ;๋ฅผ ์“ด๋‹ค. +![image](https://github.com/user-attachments/assets/b2a1393e-1bd9-41e5-8189-9cef5d7a6486) +- ์ถ”์ƒ ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/e97e8182-45b2-427f-9a53-978c36cebf5e) +- ์ถ”์ƒ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/d88f1a8a-3a67-4342-a592-f64cb6fbc8fe) +- NoteBook ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/799a67fe-1d38-4117-89ce-ffe274d5f710) +- MyNotebook ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/e8c392b6-0b6a-4e1f-bc2a-7102fa1e6bc3) +- ์ถ”์ƒ ํด๋ž˜์Šค ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/ed7929a1-e7cb-4017-bf54-0b428abfa91e) +ํ•˜์ง€๋งŒ ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šค๋กœ ์ƒ์„ฑ ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค. + +- ์ถ”์ƒํด๋ž˜์Šค์™€ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ +ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ : ํ‹€์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ +![image](https://github.com/user-attachments/assets/d38aae5b-0c5f-435d-aab8-c1c587285a58) +- AIcar ํด๋ž˜์Šค ๊ตฌํ˜„ +![image](https://github.com/user-attachments/assets/2300a96f-bc06-4592-898b-aed083535dee) +- ManualCar +![image](https://github.com/user-attachments/assets/fb9927ad-e821-4c95-abd8-26671da8680f) +- ๋‘ ์ž๋™์ฐจ๊ฐ€ ์–ด๋–ป๊ฒŒ ์›€์ง์ด๋Š”์ง€ ํ™•์ธ +![image](https://github.com/user-attachments/assets/b3db1d3c-cb41-43e6-8512-3b34773173fd) + +playerLevel ์ถ”์ƒ ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/c169f37b-7eac-4076-9637-47617524bf64) +์ดˆ๋ณด์ž ๋ ˆ๋ฒจ ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/e07b3674-49ec-4a3f-b122-640c4b460424) +์ค‘๊ธ‰์ž +![image](https://github.com/user-attachments/assets/f7db5135-c74d-4d7e-81a6-a629793f3d23) +๊ณ ๊ธ‰์ž +![image](https://github.com/user-attachments/assets/218094a1-acae-4700-815c-c0dd5c388df2) +ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/aa0b19f9-e7bf-4b47-865b-b50aa438670b) diff --git "a/\354\244\200\355\231\230/chapter10.md" "b/\354\244\200\355\231\230/chapter10.md" new file mode 100644 index 0000000..5b9a83f --- /dev/null +++ "b/\354\244\200\355\231\230/chapter10.md" @@ -0,0 +1,42 @@ + + +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1.์ธํ„ฐํŽ˜์ด์Šค๋ž€ +2. ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋‹คํ˜•์„ฑ +3. ์ธํ„ฐํŽ˜์ด์Šค ์š”์†Œ ์‚ดํŽด๋ณด๊ธฐ +4. ์ธํ„ฐํŽ˜์ด์Šค ํ™œ์šฉํ•˜๊ธฐ +### ๐Ÿง  ๋ฐฐ์šด ์  +์ธํ„ฐํŽ˜์ด์Šค๋ž€ +- ํด๋ž˜์Šค ํ˜น์€ ํ”„๋กœ๊ทธ๋žจ์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•˜๋Š” ์—ญํ•  +- ์ถ”์ƒ ๋ฉ”์„œ๋“œ์™€ ์ƒ์ˆ˜๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. +calc ์ธํ„ฐํŽ˜์ด์Šค ๋ถ€๋ถ„ ๋‹ค์‹œํ•ด์„œ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. +- scheduler ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ํ•˜๊ธฐ + + ![image](https://github.com/user-attachments/assets/ceaf39d2-bd97-4fcb-96c8-c08f96456e61) +- ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ๋ถ„ํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/99dabce0-8682-4400-a454-1c4eaa04466c) +- ์งง์€ ๋Œ€๊ธฐ์—ด ๋จผ์ € ๋ฐฐ๋ถ„ํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/73dbcee1-30db-4ed1-8204-77d3097a6624) +- ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ๋ฐฐ๋ถ„ํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/402b034b-8a0a-41c6-98e6-777ccccfea4b) +- ์ž…๋ ฅ ๋ฌธ์ž์— ๋”ฐ๋ผ ๋ฐฐ๋ถ„ ์ •์ฑ… ์ˆ˜ํ–‰ํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/11fa8ed9-a9c4-4375-ba88-efe4d61dea2b) + + ![image](https://github.com/user-attachments/assets/0e196306-2bdb-4a53-9caf-c856f880b2ed) + +- shelf ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ + + ![image](https://github.com/user-attachments/assets/f5488a2d-2388-4dae-b85f-0db5a7cca845) +- Queue ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ํ•˜๊ธฐ + + ![image](https://github.com/user-attachments/assets/8618936a-fcae-44e8-8e09-76c0216663c9) +- BookShelf ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ + + ![image](https://github.com/user-attachments/assets/d9974401-88ad-430d-9b7a-18b6ebc2c90e) +- BookShelf ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ + + ![image](https://github.com/user-attachments/assets/f3e8860c-5b7c-4efa-8742-801f2d963ef5) + ![image](https://github.com/user-attachments/assets/316f8a50-97cb-4452-9ea9-6e3fea0b8d70) + + + diff --git "a/\354\244\200\355\231\230/chapter_05.md" "b/\354\244\200\355\231\230/chapter_05.md" new file mode 100644 index 0000000..c0b1459 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_05.md" @@ -0,0 +1,48 @@ + + +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ํด๋ž˜์Šค +2. ํด๋ž˜์Šค ์‚ดํŽด๋ณด๊ธฐ +3. ๋ฉ”์„œ๋“œ +4. ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค +5. ์ƒ์„ฑ์ž +6. ์ฐธ์กฐ ์ž๋ฃŒํ˜• +### ๐Ÿง  ๋ฐฐ์šด ์  +- ํด๋ž˜์Šค ์ •์˜ + ![image](https://github.com/user-attachments/assets/e930df43-063d-402a-8fe4-4422491c243b) +- ๋ณ€์ˆ˜ ์ž๋ฃŒํ˜• +- ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•:int,long,float,double +- ์ฐธ์กฐ ์ž๋ฃŒํ˜•:String, Data, Student +- ![image](https://github.com/user-attachments/assets/9f16a709-1d18-4dcd-904a-7a18a26df88d) +- ๋ฉ”์„œ๋“œ : ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ +- ![image](https://github.com/user-attachments/assets/26856059-7cc7-4bb2-8124-bc8891b22172) +- ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ณ  ๊ฐ’ ๋ฐ˜ํ™˜ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/2bb1dac2-9a56-4397-99c8-ef88c6ecaeb8) +- ![image](https://github.com/user-attachments/assets/c91c6bb9-b45c-4594-8ac7-d3930c118f49) +- ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/b31bfed3-daba-41bd-a365-a8418c26683b) +- ํด๋ž˜์Šค์— ํ•จ์ˆ˜ ์ถ”๊ฐ€ ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/0a19c7ee-12a0-4f5d-b6e4-162c5e0bc068) +- ![image](https://github.com/user-attachments/assets/cf245be0-f71f-4337-8d7f-ad270f492a5d) +- ์ธ์Šคํ„ด์Šค : ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์ƒ์„ฑ๋œ ์ƒํƒœ -> ์ƒ์„ฑ๋œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค = ๊ฐ์ฒด +- ์ฐธ์กฐ ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/784b08d8-39fa-4560-9d4a-6b616eab4cf6) +- ![image](https://github.com/user-attachments/assets/3f6745af-eee9-45d9-9e65-db3daeae7879) +- ์ƒ์„ฑ์ž ๊ฐ์ฒด๋ฅผ ์ฒ˜์Œ ๋งŒ๋“ค ๋•Œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ +- ๋””ํดํŠธ ์ƒ์„ฑ์ž: ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ์ƒ์„ฑ์ž +- ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ, ์ƒ์„ฑ์ž ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ, ์‚ฌ์šฉํ•˜๊ธฐ, ๊ตฌํ˜„ํ•˜๊ธฐ +- ![image](https://github.com/user-attachments/assets/17b3d29b-0c64-467a-9a1b-a637af706166) +- ![image](https://github.com/user-attachments/assets/e1ddca78-45b0-40f8-9932-0ec832379d0d) +- ์ฐธ์กฐ ์ž๋ฃŒํ˜• +- ํด๋ž˜์Šค ์ž๋ฃŒํ˜•์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ +- ํด๋ž˜์Šค ์ƒ์„ฑ +- ![image](https://github.com/user-attachments/assets/c625e222-f954-41bd-8c6f-acee457be2f2) +- ๋ณ€์ˆ˜ ์ถ”๊ฐ€ +- ![image](https://github.com/user-attachments/assets/964d150c-6c17-48fc-a04f-bb31318a2e45) +- Subjectํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธ +- ![image](https://github.com/user-attachments/assets/3bfbe61d-172a-4278-bc1a-a6622b0f27b5) + +- + diff --git "a/\354\244\200\355\231\230/chapter_11.md" "b/\354\244\200\355\231\230/chapter_11.md" new file mode 100644 index 0000000..1d278bf --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_11.md" @@ -0,0 +1,235 @@ + +# 11 ๊ธฐ๋ณธํด๋ž˜์Šค +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. Object ํด๋ž˜์Šค +2. String ํด๋ž˜์Šค +3. Wrapper ํด๋ž˜์Šค +4. Class ํด๋ž˜์Šค +### ๐Ÿง  ๋ฐฐ์šด ์  +object ํด๋ž˜์Šค +- ๋ชจ๋“  ์ž๋ฐ” ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค +- ์ปดํŒŒ์ผ ๊ณผ์ •์—์„œ extends object๊ฐ€ ์ž๋™์œผ๋กœ ์“ฐ์—ฌ์ง„๋‹ค. +- Object ๋ฉ”์„œ๋“œ + | ๋ฉ”์„œ๋“œ | ์„ค๋ช… | + | --- | --- | + | String toString( ) | ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๋ฐ˜ํ™˜, ์žฌ์ •์˜ํ•˜์—ฌ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‚˜ ํŠน์ • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ | + | boolean equals(Object obj) | ๋‘ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋™์ผํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์žฌ์ •์˜ํ•˜์—ฌ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค์ž„์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. | + | int hashCode( ) | ๊ฐ์ฒด์˜ ํ•ด์‹œ ์ฝ”๋“œ ๊ฐ’์„ ๋ฐ˜ํ™˜ | + | Object clone( ) | ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•˜์—ฌ ๋™์ผํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ | + | Class getClass( ) | ๊ฐ์ฒด์˜ Class ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ | + | void finalize( ) | ์ธ์Šคํ„ด์Šค๊ฐ€ ํž™ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐ๋  ๋•Œ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ, ๋„คํŠธ์›Œํฌ ํ•ด์ œ, ์—ด๋ ค ์žˆ๋Š” ํŒŒ์ผ ์ŠคํŠธ๋ฆผ ํ•ด์ฒด ๋“ฑ ๊ตฌํ˜„ | + | void wait( ) | ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ, ์Šค๋ ˆ๋“œ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ๋กœ ๋งŒ๋“ฌ | + | void notify( ) | wai( ) ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋Š” ์Šค๋ ˆ๋“œ๋ฅผ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๋กœ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค | + +- Object ํด๋ž˜์Šค์˜ toString( ) ๋ฉ”์„œ๋“œ + - ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค์˜ ํด๋ž˜์Šค ์ด๋ฆ„๊ณผ ์ฃผ์†Œ๊ฐ’์„ ๋ณด์—ฌ์ค€๋‹ค +![image](https://github.com/user-attachments/assets/040dc097-4bc1-4f8a-acff-e2181b0ced24) +![image](https://github.com/user-attachments/assets/0773d725-e313-458d-b249-6ad6d1d8e51d) + - toString( ) ๋ฉ”์„œ๋“œ ์ •์˜ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/87f9d888-0cb1-4e2b-a082-8066cda57aaf) +![image](https://github.com/user-attachments/assets/cf685a83-7d19-40ea-8c25-d293823afe94) + +- equals( ) ๋ฉ”์„œ๋“œ + - ๋‘ ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ์„ ๋•Œ `==` ๋Š” ๋‹จ์ˆœํžˆ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ ํ•  ์ˆ˜์žˆ๊ณ , ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ™•์ธํ•˜๋„๋ก ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +``` +package chapter11.object; + +class Student { + int studentId; + String studentName; + + public Student(int studentid, String studentName) { + this.studentId = studentid; + this.studentName = studentName; + } + + public String toString() { + return studentId + "," + studentName; + } +} + +public class EqualsTest { + public static void main(String[] args) { + Student studentLee = new Student(100, "์ด์ƒ์›"); + Student studentLee2 = studentLee; //์ฃผ์†Œ ๋ณต์‚ฌ + Student studentSang = new Student(100, "์ด์ƒ์›"); + + if(studentLee == studentLee2) // == ๊ธฐํ˜ธ๋กœ ๋น„๊ต + System.out.println("studentLee์™€ studentLee2์˜ ์ฃผ์†Œ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค."); + else + System.out.println("studentLee์™€ studentLee2์˜ ์ฃผ์†Œ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค."); + + if(studentLee.equals(studentLee2)) // equals() ๋ฉ”์„œ๋“œ๋กœ ๋น„๊ต + System.out.println("studentLee์™€ studentLee2์˜ ์ฃผ์†Œ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค."); + else + System.out.println("studentLee์™€ studentLee2์˜ ์ฃผ์†Œ๋Š” ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + + if(studentLee == studentSang)// == ๊ธฐํ˜ธ๋กœ ๋น„๊ต + System.out.println("studentLee์™€ studentSang์˜ ์ฃผ์†Œ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค."); + else + System.out.println("studentLee์™€ studentSang์˜ ์ฃผ์†Œ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค."); + + if(studentLee.equals(studentSang))// equals() ๋ฉ”์†Œ๋“œ๋กœ ๋น„๊ต๊ต + System.out.println("studentLee์™€ studentSang์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค."); + else + System.out.println("studentLee์™€ studentSang์€ ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } +} + +``` +![image](https://github.com/user-attachments/assets/66cb370b-edcd-4e86-8687-ea82a20357d6) + + - String๊ณผ Integer ํด๋ž˜์Šค์˜ equals()๋ฉ”์„œ๋“œ +![image](https://github.com/user-attachments/assets/2cb5603f-72e2-4c61-9ef0-0e0788ea1839) +![image](https://github.com/user-attachments/assets/dccf3834-1685-4e8d-bb07-65f0505db69f) + + - equals() ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/83eebda9-11dc-4592-a4d9-91a47788bc04) + +- hashCode()๋ฉ”์„œ๋“œ + - ๊ฐ์ฒด์˜ ํŠน์ • ์ •๋ณด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์œผ๋กœ ๋„ฃ์œผ๋ฉด ๊ทธ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋˜์–ด์•ผ ํ•  ์œ„์น˜๋‚˜ ์ €์žฅ๋œ ํ•ด์‹œ ํ…Œ์ด๋ธ” ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ ํ•ฉ๋‹ˆ๋‹ค + - ๊ฐ์ฒด ์ •๋ณด๋ฅผ ์•Œ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด์˜ ์œ„์น˜๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. + + - String๊ณผ Integer ํด๋ž˜์Šค์˜ hashCode() ๋ฉ”์„œ๋“œ +![image](https://github.com/user-attachments/assets/a459b6bc-edbe-4402-8fb5-e07aef889eec) +![image](https://github.com/user-attachments/assets/73df6209-d274-4ab2-a579-c915f2a93691) + + - hashCode() ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/a76d85bd-e692-4487-8831-4c66148b3f6b) +![image](https://github.com/user-attachments/assets/0d81cd9b-7d81-45ec-a73f-823a011ff786) + + +- clone() ๋ฉ”์„œ๋“œ + - ๊ฐ€๋ณธ ํ‹€์˜ ๋ณต์‚ฌ๋ณธ์„ ์‚ฌ์šฉํ•ด ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณต์žกํ•œ ์ƒ์„ฑ ๊ณผ์ •์„ ๊ฐ„๋‹จํžˆ ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ + + - clone ๋ฉ”์„œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค ๋ณต์ œํ•˜๊ธฐ +``` +package chapter11.object; +//์›์ ์„ ์˜๋ฏธํ•˜๋Š” Point ํด๋ž˜์Šค +class Point { + int x; + int y; + + Point(int x, int y) { + this.x = x; + this.y = y; + } + + public String toString() { + return "x = " + x + ", " + "y = " + y; + } +} +// ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•ด๋„ ๋œ๋‹ค๋Š” ์˜๋ฏธ๋กœ Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ•จ๊ป˜ ์„ ์–ธ +class Circle implements Cloneable { + Point point; + int radius; + + Circle(int x, int y, int radius) { + this.radius = radius; + point = new Point(x, y); + } + + public String toString() { + return "์›์  = " + point + ", " + "๋ฐ˜์ง€๋ฆ„์€ " + radius + "์ž…๋‹ˆ๋‹ค."; + } +//clone() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•จํ•จ + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} + +public class ObjectCloneTest { + public static void main(String[] args) throws CloneNotSupportedException { + Circle circle = new Circle(10, 20, 30); + Circle copyCircle = (Circle)circle.clone(); // clone() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด circle ์ธ์Šคํ„ด์Šค๋ฅผ copyCircle์— ๋ณต์ œํ•จํ•จ + + System.out.println(circle); + System.out.println(copyCircle); + System.out.println(System.identityHashCode(circle)); + System.out.println(System.identityHashCode(copyCircle)); + } +} + +``` +![image](https://github.com/user-attachments/assets/664f0bdb-3e77-40ca-b2b3-d0b9a31836cf) + +- String ํด๋ž˜์Šค + - ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•˜์—ฌ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹ + - ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด ์ƒ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐฉ์‹ + +- ์ฃผ์†Œ ๊ฐ’ ๋น„๊ตํ•˜๊ธฐ + +![image](https://github.com/user-attachments/assets/16cda20a-35b3-4ebf-8f49-27f2582ccdca) +![image](https://github.com/user-attachments/assets/0890f9bb-a6e0-4614-9ace-1250abe46910) + +- ๋‘ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐํ•˜๊ธฐ +- ๋‘ ๋ฌธ์ž์—ด์ด ์—ฐ๊ฒฐ๋ ๋•Œ ๋ณ€๊ฒฝ์ด ๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋‘ ๋ฌธ์ž์—ด์ด ์—ฐ๊ฒฐ๋œ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ + +![image](https://github.com/user-attachments/assets/940922e7-5b37-4492-a87f-1a73016997f9) +![image](https://github.com/user-attachments/assets/e583a462-62e1-4941-a7b5-1ace0bdbd0e1) + +- StringBuilder ํด๋ž˜์Šค +- ๋‚ด๋ถ€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ Char[]๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. +- ๊ธฐ์กด ์‚ฌ์šฉํ•˜๋˜ char[] ๋ฐฐ์—ด์ด ํ™•์žฅ๋˜๋ฏ€๋กœ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. + +![image](https://github.com/user-attachments/assets/505b5214-307e-4094-8446-787588deefe2) +![image](https://github.com/user-attachments/assets/96d849e5-f642-4bed-8795-6974a087d695) + +- Class ํด๋ž˜์Šค๋ž€? +- ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์— ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ์ฐพ์„๋•Œ ์‚ฌ์šฉ + 1. Object ํด๋ž˜์Šค์˜ getClass() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ + 2. ํด๋ž˜์Šค ํŒŒ์ผ ์ด๋ฆ„์„ Class ๋ณ€์ˆ˜์— ์ง์ ‘ ๋Œ€์ž…ํ•˜๊ธฐ + 3. Class.forName("ํด๋ž˜์Šค ์ด๋ฆ„") ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ + +- ํด๋ž˜์Šค ์ƒ์„ฑ +``` +package chapter11.classex; +public class Person { + private String name; + private int age; + + public Person() {} + + public Person(String name) { + this.name = name; + } + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} + +``` +- Person์˜ Class ํด๋ž˜์Šค ๊ฐ€์ ธ์˜ค๊ธฐ + +![image](https://github.com/user-attachments/assets/3e187a41-a6b6-46e8-be56-72278c19d504) +![image](https://github.com/user-attachments/assets/50c2ae1a-a0d6-49f3-8b07-15ec16a6ae5e) + +- String ํด๋ž˜์Šค ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ + +![image](https://github.com/user-attachments/assets/4c5d9d9e-94f6-4034-a78f-2220d81c88e9) + +- Person ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ + +![image](https://github.com/user-attachments/assets/a3d33ec1-d116-46e3-8962-474f9dd446c5) +![image](https://github.com/user-attachments/assets/f5cd9294-c6e7-4d77-b1d5-320321190ca3) + + diff --git "a/\354\244\200\355\231\230/chapter_12.md" "b/\354\244\200\355\231\230/chapter_12.md" new file mode 100644 index 0000000..f2ff63c --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_12.md" @@ -0,0 +1,404 @@ + +# 12 ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ์ œ๋„ค๋ฆญ +2. ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ +3. List ์ธํ„ฐํŽ˜์ด์Šค +4. Set ์ธํ„ฐํŽ˜์ด์Šค +5. Map ์ธํ„ฐํŽ˜์ด์Šค +## ๐Ÿง  ๋ฐฐ์šด ์  +### ์ œ๋„ค๋ฆญ์ด๋ž€? + - ์–ด๋–ค ๊ฐ’์ด ํ•˜๋‚˜์˜ ์ฐธ์กฐ ์ž๋ฃŒํ˜•์ด ์•„๋‹Œ ์—ฌ๋Ÿฌ ์ฐธ์กฐ ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ์„ ์ œ๋„ค๋ฆญ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. + +- Powder,Plastic,GenericPrinterํด๋ž˜์„œ ์ •์˜ํ•˜๊ธฐ + +![image](https://github.com/user-attachments/assets/e92ca533-95c2-4fe8-9fb7-d4091a53c4c2) +![image](https://github.com/user-attachments/assets/a8b2a94a-11d8-4727-96c1-2b241e74d115) +![image](https://github.com/user-attachments/assets/301f9f39-4f08-4414-958d-732102e7639b) + +- GenericPrinter ํด๋ž˜์Šค ์‚ฌ์šฉํ•˜๊ธฐ +- ์ž๋ฃŒํ˜•์„ ๋ช…์‹œํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ +![image](https://github.com/user-attachments/assets/c8462d30-4f10-43bb-9d1a-1584a05809e2) +- ์ž๋ฃŒํ˜•์„ ๋ช…์‹œํ•œ ๊ฒฝ์šฐ +![image](https://github.com/user-attachments/assets/7ec00ffc-a374-46ef-bbdc-deaa542e0e0a) +![image](https://github.com/user-attachments/assets/a80bdc5e-7569-4201-8630-25f16dacddca) + +- ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค์—์„œ T ์ž๋ฃŒํ˜•์— ์‚ฌ์šฉํ•  ์ž๋ฃŒํ˜•์— ์ œํ•œ์„ ๋‘๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅ +- 3D ์ถœ๋ ฅ์—์„œ ๋ฌผ์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์—†๋Š”๊ฑฐ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค์— ์ž๋ฃŒํ˜• ์ œํ•œ์„ ๋‘๋Š” ๋ฐฉ์‹์œผ๋กœ extends ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. +- ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ ์ƒ์†์„ ๋ฐ›์Œ + +- Material ์ถ”์ƒ ํด๋ž˜์Šค +![image](https://github.com/user-attachments/assets/336acc70-def8-407b-8628-809ff433c072) +- ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ extend๋กœ ๋ฐ›์•„์“ด๋‹ค. + +- ์ž๋ฃŒํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ ์˜ˆ์ œ +- ์ž๋ฃŒํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‘ ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค +![image](https://github.com/user-attachments/assets/351cce22-b933-4392-b418-646ae09039bb) + +- ์ œ๋„ค๋ฝ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/8ceeb4bd-781d-4952-9efa-2561bab057bc) +![image](https://github.com/user-attachments/assets/745cf910-592a-42af-9324-8cc52ff66c15) + +### ์ปฌ๋Ÿฌ ํ”„๋ ˆ์ž„ ์›Œํฌ๋ž€? +- ์ž๋ฃŒ๋ฅผ ์–ด๋–ค ๊ตฌ์กฐ๋กœ ๊ด€๋ฆฌํ•  ๊ฒƒ์ธ๊ฐ€? -> ์ž๋ฃŒ๊ตฌ์กฐ +- ์ž๋ฐ”์—์„œ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•˜์—ฌ java.util ํŒจํ‚ค์ง€์—์„œ ์ œ๊ณตํ•œ๋‹ค. -> ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ +- Collection ์ธํ„ฐํŽ˜์ด์Šค + - List๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋Š” ์ˆœ์ฐจ์ ์ธ ์ž๋ฃŒ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค + - Set ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. + ![image](https://github.com/user-attachments/assets/fef0c348-039a-4a3f-a391-eddc658dcb70) + + | ๋ฉ”์„œ๋“œ | ์„ค๋ช… | + | --- | --- | + | boolean add(E e) | Collection์— ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค | + | void clear() | Collection์˜ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐ | + | Iterator Iterator | Collection๋ฅผ ์ˆœํ™˜ํ•  ๋ฐ˜๋ณต์ž๋ฅผ ๋ฐ˜ํ™˜ | + | boolean reamove(Object o) | Collection์— ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ œ๊ฑฐ | + | int size | Collection์— ์žˆ๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ | + +- Map ์ธํ„ฐํŽ˜์ด์Šค + - ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ ์Œ์œผ๋กœ ๋˜์–ด ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด ์„ ์–ธ๋˜์–ด ์žˆ๋‹ค. + - key-value ์Œ์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์ด๋•Œ ํ‚ค ๊ฐ’์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†๋‹ค. + + | ๋ฉ”์„œ๋“œ | ์„ค๋ช… | + | --- | --- | + | put(K key, V value) | key์— ํ•ด๋‹นํ•˜๋Š” value ๊ฐ’์„ map์— ๋„ฃ์Šต๋‹ˆ๋‹ค. | + | get(K key) | key์— ํ•ด๋‹นํ•˜๋Š” value ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + | boolean isEmpty() | Map์ด ๋น„์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + | boolean containsKey(Object key) | Map์— ํ•ด๋‹น key๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + | boolean containsValue(Object value) | Map์— ํ•ด๋‹น value๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. | + | Set keySet() | key ์ง‘ํ•ฉ์„ Set์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค (์ค‘๋ณต ์•ˆ ๋˜๋ฏ€๋กœ Set). | + | Collection values() | value๋ฅผ Collection์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค (์ค‘๋ณต ๋ฌด๊ด€). | + | remove(K key) | key๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. | + | boolean remove(Object key, Object value) | key๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ key์— ํ•ด๋‹นํ•˜๋Š” value๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•  ๋•Œ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. + + - Member ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/cc04eb07-7696-40fd-9959-5309bd123a38) + +- ArrayList ํ™œ์šฉํ•˜๊ธฐ +``` +package chapter12.collection.arraylist; + +import java.util.ArrayList; +import chapter12.collection.Member; + +public class MemberArrayList { + private ArrayList arrayList; //ArrayList ์„ ์–ธ + + public MemberArrayList() { + arrayList = new ArrayList(); // Memberํ˜•์œผ๋กœ ์„ ์–ธํ•œ ArrayList ์ƒ์„ฑ + } + //ArrayList์— ํšŒ์›์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“œ + public void addMember (Member member) { + arrayList.add(member); + } + //ํ•ด๋‹น ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ํšŒ์›์„ ArrayList์—์„œ ์ฐพ์•„ ์ œ๊ฑฐํ•จํ•จ + public boolean removeMember(int memberId) { + for(int i = 0; i < arrayList.size(); i++) { + Member member = arrayList.get(i); // get() ๋ฉ”์„œ๋“œ๋กœ ํšŒ์›์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ด + int tempId = member.getMemberId(); + if(tempId == memberId) { // ํšŒ์› ์•„์ด๋””๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•˜๋ฉด + arrayList.remove(i); // ํ•ด๋‹น ํšŒ์›์„ ์‚ญ์ œ์ œ + return true; + } + } + System.out.println(memberId + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); // ๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ํ•ด๋‹น ์•„์ด๋””๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ + return false; + } + // ์ „์ฒดํšŒ์›์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์„œ๋“œ + public void showAllMember() { + for(Member member : arrayList) { + System.out.println(member); + } + System.out.println(); + } +} + +``` +- MemberArrayListTest ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/db4c6b62-1a38-4736-b850-791b49dd8d21) +![image](https://github.com/user-attachments/assets/54c06b8a-dec7-428b-9593-22886b90821f) + +- LinkedList ํด๋ž˜์Šค + - ๊ฐ ์š”์†Œ๋Š” ๋‹ค์Œ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. + - ๋”ฐ๋ผ์„œ ๋ฌผ๋ฆฌ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋–จ์–ด์ ธ ์žˆ์–ด๋„ ๋…ผ๋ฆฌ์ ์œผ๋กœ๋Š” ์•ž ๋’ค ์ˆœ์„œ๊ฐ€ ์กด์žฌ + - ์ค‘๊ฐ„์— ์ž๋ฃŒ๋ฅผ ๋„ฃ๊ณ  ์ œ๊ฑฐํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฐ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. + - ํฌ๊ธฐ๋ฅผ ๋™์ ์œผ๋กœ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. + +- LinkedList ํ…Œ์ŠคํŠธํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/529a63f5-c073-4656-91e2-3a2697eb428e) + +- ์Šคํƒ ๊ตฌํ˜„ํ•˜๊ธฐ +``` +package chapter12.collection.arraylist; + +import java.util.ArrayList; + +class MyStack { + private ArrayList arrayStack = new ArrayList(); + //์Šคํƒ์˜ ๋งจ ๋’ค์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ + public void push(String data) { + arrayStack.add(data); + } + //์Šคํƒ์˜ ๋งจ ๋’ค์—์„œ ์š”์†Œ ๊บผ๋ƒ„ + public String pop() { + int len = arrayStack.size();//ArrayList์— ์ €์žฅ๋œ ์œ ํšจํ•œ ์ž๋ฃŒ์˜ ๊ฐœ์ˆ˜ + if(len == 0) { + System.out.println("์Šคํƒ์ด ๋น„์—ˆ์Šต๋‹ˆ๋‹ค."); + return null; + } + return(arrayStack.remove(len-1));// ๋งจ ๋’ค์— ์žˆ๋Š” ์ž๋ฃŒ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐ๊ฑฐ + } +} + +public class StackTest { + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push("A"); + stack.push("B"); + stack.push("C"); + + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + } +} + +``` +![image](https://github.com/user-attachments/assets/4fe2f9a7-3693-4b9c-a41d-5b883ab13435) + +- ํ ๊ตฌํ˜„ํ•˜๊ธฐ +``` +package chapter12.collection.arraylist; + +import java.util.ArrayList; + +class MyQueue { + private ArrayList arrayQueue = new ArrayList(); + // ํ์˜ ๋งจ ๋’ค์— ์ถ”๊ฐ€ + public void enQueue(String data) { + arrayQueue.add(data); + } + // ํ์˜ ๋งจ ์•ž์—์„œ ๊บผ๋ƒ„๋ƒ„ + public String deQueue() { + int len = arrayQueue.size(); + if(len == 0) { + System.out.println("ํ๊ฐ€ ๋น„์—ˆ์Šต๋‹ˆ๋‹ค"); + return null; + } + return(arrayQueue.remove(0)); // ๋งจ ์•ž์˜ ์ž๋ฃŒ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐ + } +} +public class QueueTest { + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue("A"); + queue.enQueue("B"); + queue.enQueue("C"); + + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + } +} + +``` +![image](https://github.com/user-attachments/assets/2d6bfe38-3458-4582-a3a1-e81a03fd2178) + +- HashSet ํด๋ž˜์Šค + - ์ง‘ํ•ฉ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉฐ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. + +- HashSet ํ…Œ์ŠคํŠธํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/9d586b52-9704-42e7-a3d0-4c688499bfe6) +![image](https://github.com/user-attachments/assets/f038bee1-6b17-423b-8f5e-b54845e3c9d4) + +- HashSet ํ™œ์šฉํ•˜๊ธฐ +``` +package chapter12.collection.hashset; + +import java.util.HashSet; +import java.util.Iterator; + +import chapter12.collection.Member; +public class MemberHashSet { + private HashSet hashSet; + + public MemberHashSet() { + hashSet = new HashSet(); + } + + public void addMember(Member member) { + hashSet.add(member); + } + + public boolean removeMember(int memberId) { + Iterator ir = hashSet.iterator(); + + while(ir.hasNext()) { + Member member = ir.next(); + int tempId = member.getMemberId(); + if(tempId == memberId) { + hashSet.remove(member); + return true; + } + } + System.out.println(memberId + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return false; + } + + public void showAllMember() { + for(Member member : hashSet) { + System.out.println(member); + } + System.out.println(); + } +} + +``` +![image](https://github.com/user-attachments/assets/662000b7-99ad-464c-ace1-bce94a2f9c40) +![image](https://github.com/user-attachments/assets/c351a2cf-c7a8-4726-972b-4b1cbee4d4c5) + +- TreeSet ํด๋ž˜์Šค + - ์ž๋ฃŒ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ •๋ ฌํ•˜๋Š” ํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค. +![image](https://github.com/user-attachments/assets/bb8d3bc8-af31-4de9-bec3-3de1f28a1563) +![image](https://github.com/user-attachments/assets/8f35b362-220b-4532-a6f6-34f52455e2d3) +``` +package chapter12.collection.treeset; + +import java.util.Iterator; +import java.util.TreeSet; + +import chapter12.collection.Member; + +public class MemberTreeSet { + private TreeSet treeSet; + + public MemberTreeSet() { + treeSet = new TreeSet(); + } + //ํšŒ์›์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ + public void addMember(Member member) { + treeSet.add(member); + } + // ํšŒ์›์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“œ + public boolean removeMember(int memberId) { + Iterator ir = treeSet.iterator(); + + while(ir.hasNext()) { + Member member = ir.next(); + int tempId = member.getMemberId(); + if(tempId == memberId) { + treeSet.remove(member); + return true; + } + } + System.out.println(memberId + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return false; + } + //์ „์ฒด ํšŒ์›์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“œ + public void showAllMember() { + for(Member member : treeSet) { + System.out.println(member); + } + System.out.println(); + } +} + +``` +![image](https://github.com/user-attachments/assets/dcbc96c0-9634-4c75-8e58-ecd95e165586) + +- Comparable ์ธํ„ฐํŽ˜์ด์Šค + - ์ž๊ธฐ ์ž์‹ ๊ณผ ์ „๋‹ฌ๋ฐ›์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋น„๊ตํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค +![image](https://github.com/user-attachments/assets/0561ffd2-611e-44de-9613-7fb3f96c1d4c) + +![image](https://github.com/user-attachments/assets/22e51f91-15ee-4eb3-a13b-780dba8d6433) + +- HashMap ํด๋ž˜์Šค +- HashMap ํ™œ์šฉํ•˜๊ธฐ +``` +package chapter12.collection.hashmap; + +import java.util.HashMap; +import java.util.Iterator; + +import chapter12.collection.Member; + +public class MemberHashMap { + private HashMap hashMap; + + public MemberHashMap() { + hashMap = new HashMap(); + } + //ํšŒ์›์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ + public void addMember(Member member) { + hashMap.put(member.getMemberId(), member);//key-value ์Œ์œผ๋กœ ์ถ”๊ฐ€๊ฐ€ + } + //ํšŒ์›์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“œ + public boolean removeMember(int memberId) { + if(hashMap.containsKey(memberId)) { // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ํ‚ค ๊ฐ’์ธ ํšŒ์› ์•„์ด๋””๊ฐ€ ์žˆ๋‹ค๋ฉด + hashMap.remove(memberId);// ํ•ด๋‹น ํšŒ์› ์‚ญ์ œ์ œ + return true; + } + System.out.println(memberId + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return false; + } + //Iterator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ํšŒ์›์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์„œ๋“œ + public void showAllMember() { + Iterator ir = hashMap.keySet().iterator(); + while (ir.hasNext()) { + int key = ir.next(); + Member member = hashMap.get(key); + System.out.println(member); + } + System.out.println(); + } +} + +``` +![image](https://github.com/user-attachments/assets/d5d56eaa-bbeb-4b45-a889-e0558c37bb64) +![image](https://github.com/user-attachments/assets/87bf6ee7-5fdc-4815-9f63-cd568a6b7800) + +- TreeMap ํด๋ž˜์Šค + - key ๊ฐ’์œผ๋กœ ์ž๋ฃŒ๋ฅผ ์ •๋ ฌํ• ๋ ค๋ฉด ์‚ฌ์šฉ +``` +package chapter12.collection.treemap; + +import java.util.Iterator; +import java.util.TreeMap; + +import chapter12.collection.Member; +public class MemberTreeMap { + private TreeMap treeMap; + + public MemberTreeMap() { + treeMap = new TreeMap(); + } + + public void addMember(Member member) { + treeMap.put(member.getMemberId(), member);//key-value ์Œ์œผ๋กœ ์ถ”๊ฐ€๊ฐ€ + } + + public boolean removeMember(int memberId) { + if(treeMap.containsKey(memberId)){ + treeMap.remove(memberId);//key ๊ฐ’์— ๋งž๋Š” ์ž๋ฃŒ ์‚ญ์ œ์ œ + return true; + } + System.out.println(memberId + "๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + return false; + + } + + public void showAllMember() { + Iterator ir = treeMap.keySet().iterator(); + while (ir.hasNext()) { + int key = ir.next(); + Member member = treeMap.get(key); + System.out.println(member); + } + System.out.println(); + } +} + + +``` +![image](https://github.com/user-attachments/assets/fae73b33-5105-4368-8348-c2b680f0a677) +![image](https://github.com/user-attachments/assets/afb0ca8c-67ac-46c0-8650-41a81a1a2dab) diff --git "a/\354\244\200\355\231\230/chapter_13.md" "b/\354\244\200\355\231\230/chapter_13.md" new file mode 100644 index 0000000..8b76aa9 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_13.md" @@ -0,0 +1,225 @@ + +# 12 ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ๋‚ด๋ถ€ ํด๋ž˜์Šค +2. ๋žŒ๋‹ค์‹ +3. ์ŠคํŠธ๋ฆผ +## ๐Ÿง  ๋ฐฐ์šด ์  +### ๋‚ด๋ถ€ ํด๋ž˜์Šค +- ์„ ์–ธํ•˜๋Š” ์œ„์น˜๋‚˜ ์˜ˆ์•ฝ์–ด์— ๋”ฐ๋ผ ๋„ค๊ฐ€์ง€ ์œ ํ˜• + 1. ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค + 2. ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค + 3. ์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค + 4. ์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค + +- ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค + - ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ์™€ ๊ฐ™์€ ์œ„์น˜์— ์„ ์–ธํ•˜๋ฉฐ, ์™ธ๋ถ€ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉ + - ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. + +- ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค +``` +package chapter13.innerclass; + +class OutClass { + private int num = 10; + private static int sNum = 20; + + private InClass inClass;// ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜๋ฅผ ๋จผ์ € ์„ ์–ธ + + // ์™ธ๋ถ€ ํด๋ž˜์Šค ๋””ํดํŠธ ์ƒ์„ฑ์ž. ์™ธ๋ถ€ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋œ ํ›„์— ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ƒ์„ฑ๊ฐ€๋Šฅ๋Šฅ + public OutClass() { + inClass = new InClass(); + } + + class InClass { + int inNum = 100; + + void inTest() { + System.out.println("OutClass num = " + num + " (์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜)"); + System.out.println("OutClass sNum = " + sNum + " (์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜)"); + } + } + + public void usingClass() { + inClass.inTest(); + } +} + +public class InnerTest { + public static void main(String[] args) { + OutClass outClass = new OutClass(); + System.out.println("์™ธ๋ถ€ ํด๋ž˜์Šค ์ด์šฉํ•˜์—ฌ ๋‚ด๋ถ€ ํด๋ž˜์Šค ๊ธฐ๋Šฅ ํ˜ธ์ถœ"); + outClass.usingClass();// ๋‚ด๋ถ€ ํด๋ž˜์Šค ๊ธฐ๋Šฅ ํ˜ธ์ถœ์ถœ + } +} + +``` +![image](https://github.com/user-attachments/assets/72c020cc-51ad-40b9-9db5-d77c1b1c224b) + +- ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค + - ๋‚ด๋ถ€ ํด๋ž˜์Šค๊ฐ€ ์™ธ๋ถ€ ํด๋ž˜์Šค ์ƒ์„ฑ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ  ์ •์  ๋ณ€์ˆ˜๋„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋ฉด ์‚ฌ์šฉ + - ๋‚ด๋ถ€ ํด๋ž˜์Šค์ฒ˜๋Ÿผ ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ์œ„์น˜์— ์ •์˜ํ•˜๋ฉฐ static ์˜ˆ์•ฝ์–ด๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. + +``` +package chapter13.innerclass; + +class OutClass { + private int num = 10; + private static int sNum = 20; + + static class InStaticClass { + int inNum = 100; + static int sInNum = 200; + //์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ๋“œ + void inTest() { + System.out.println("InStaticClass inNum" +inNum+ "(๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์‚ฌ์šฉ)"); + System.out.println("InStaticClass sInNum=" + sInNum + "(๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜ ์‚ฌ์šฉ)"); + System.out.println("OutClass sNum=" +sNum + "(์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜ ์‚ฌ์šฉ)"); + } + // ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ฉ”์„œ๋“œ๋“œ + static void sTest() { + System.out.println("OutClass sNum="+sNum+"(์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜ ์‚ฌ์šฉ)"); + System.out.println("InStaticClass sInNum="+sInNum+"์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜ ์‚ฌ์šฉ"); + } + } +} + +public class InnerTest2 { + public static void main(String[] args) { + OutClass.InStaticClass sInClass = new OutClass.InStaticClass(); // ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ƒ์„ฑ ๊ฐ€๋Šฅ๋Šฅ + System.out.println("์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ"); + sInClass.inTest(); + + System.out.println(); + System.out.println("์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ"); + OutClass.InStaticClass.sTest(); + } +} + +``` +![image](https://github.com/user-attachments/assets/2ae1a9d3-e23d-4d7e-8945-ad73389a661e) + +| ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ | ๋ณ€์ˆ˜ ์œ ํ˜• | ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ | +| --- | --- | --- | +| **์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ** void inTest() | ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(num) | โœ• | +| | ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜(sNum) | โ—‹ | +| | ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(inNum) | โ—‹ | +| | ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜(sInNum) | โ—‹ | +| **์ •์  ๋ฉ”์„œ๋“œ** static void sTest() | ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(num) | โœ• | +| | ์™ธ๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜(sNum) | โ—‹ | +| | ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜(inNum) | โœ• | +| | ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜(sInNum) | โ—‹ | + +- ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์  ๋ฉ”์„œ๋“œ์ธ ๊ฒฝ์šฐ์—๋Š” ์™ธ๋ถ€ ํด๋ž˜์Šค์™€ ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜ ์ค‘ ์ •์  ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ + + +- ์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค + - ์ง€์—ญ ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์— ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ๋งํ•œ๋‹ค. + +``` +package chapter13.innerclass; + +class Outer { + int outNum = 100; + static int sNum = 200; + + Runnable getRunnable(int i) { + int num = 100; + + class MyRunnable implements Runnable { + int localNum = 10; + + @Override + public void run() { + System.out.println("i=" + i); + System.out.println("num=" +num); + System.out.println("localNum=" +localNum); + System.out.println("outNum=" +outNum + "(์™ธ๋ถ€ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜)"); + System.out.println("Outer.sNUm=" + Outer.sNum + "(์™ธ๋ถ€ ํด๋ž˜์Šค ์ •์  ๋ณ€์ˆ˜)"); + } + } + return new MyRunnable(); + } +} +public class LocallnnerTest { + public static void main(String[] args) { + Outer out = new Outer(); + Runnable runner = out.getRunnable(10); + runner.run(); + } +} + +``` +![image](https://github.com/user-attachments/assets/bed7475c-413d-4e0f-be49-d04687a33b38) + + +- ์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค + - ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค + +![image](https://github.com/user-attachments/assets/1d8fd07f-9aa7-4f07-986c-57f4e871ed17) +![image](https://github.com/user-attachments/assets/208b97d3-eb51-41fa-9cef-15bf6fd42fea) + +๋‚ด๋ถ€ ํด๋ž˜์Šค ํ‘œ ์ •๋ฆฌ +| ์ข…๋ฅ˜ | ๊ตฌํ˜„ ์œ„์น˜ | ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์™ธ๋ถ€ ํด๋ž˜์Šค ๋ณ€์ˆ˜ | ์ƒ์„ฑ ๋ฐฉ๋ฒ• | +| --- | --- | --- | --- | +| ์ธ์Šคํ„ด์Šค ๋‚ด๋ถ€ ํด๋ž˜์Šค | ์™ธ๋ถ€ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋™์ผ | ์™ธ๋ถ€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, ์™ธ๋ถ€ ์ •์  ๋ณ€์ˆ˜ | ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ๋งŒ๋“  ํ›„ ๋‚ด๋ถ€ ํด๋ž˜์Šค ์ƒ์„ฑ | +| ์ •์  ๋‚ด๋ถ€ ํด๋ž˜์Šค | ์™ธ๋ถ€ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋™์ผ | ์™ธ๋ถ€ ์ •์  ๋ณ€์ˆ˜ | ์™ธ๋ถ€ ํด๋ž˜์Šค์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ƒ์„ฑ | +| ์ง€์—ญ ๋‚ด๋ถ€ ํด๋ž˜์Šค | ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์— ๊ตฌํ˜„ | ์™ธ๋ถ€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, ์™ธ๋ถ€ ์ •์  ๋ณ€์ˆ˜ | ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ƒ์„ฑ | +| ์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค | ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์— ๊ตฌํ˜„, ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜์—ฌ ์ง์ ‘ ๊ตฌํ˜„ | ์™ธ๋ถ€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, ์™ธ๋ถ€ ์ •์  ๋ณ€์ˆ˜ | ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ƒ์„ฑ. ๋˜๊ฑฐ๋‚˜, ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž… ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•  ๋•Œ new ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ | + +### ๋žŒ๋‹ค์‹ +- ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ +- ๋žŒ๋‹ค์‹ ๋ฌธ๋ฒ• (๋งค๊ฐœ๋ณ€์ˆ˜) -> {์‹คํ–‰๋ฌธ;} + +- ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/2405be59-ac45-4997-9ce7-795ab76592b8) + +- ๋žŒ๋‹ค์‹ ๊ตฌํ˜„๊ณผ ํ˜ธ์ถœ +![image](https://github.com/user-attachments/assets/0662dd5d-6c0c-45fc-82ab-4676a9fbbbbf) +![image](https://github.com/user-attachments/assets/62535ec0-56a4-4b39-b58e-2c171608fa1d) + +- ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค + - ์ž๋ฐ”์—์„œ ์ฐธ์กฐ ๋ณ€์ˆ˜ ์—†์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜๋Š” ์—†๋‹ค + - ๋žŒ๋‹ค์‹์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค ์ œ์ž‘ -> ์ธํ„ฐํŽ˜์ด์Šค์— ๋žŒ๋‹ค์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ๋ฉ”์„œ๋“œ ์„ ์–ธ + - ๋žŒ๋‹ค์‹์€ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ธํ„ฐํŽ˜์ด์Šคํ˜• ๋ณ€์ˆ˜์— ๋Œ€๋ฆฝํ•˜๋ฏ€๋กœ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ ธ์„œ๋Š” ์•ˆ๋œ๋‹ค. + +- ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๋žŒ๋‹ค์‹ ๋น„๊ต + +- ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ +![image](https://github.com/user-attachments/assets/b6126e35-ac65-42b6-b502-7db88b21d195) + +- ์ถ”์ƒ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ +![image](https://github.com/user-attachments/assets/73f288ae-9336-4e57-9c9b-1451b9f8fcad) + +- ๋ฉ”์„œ๋“œ ํ…Œ์ŠคํŠธ +![image](https://github.com/user-attachments/assets/ab8d62c5-fee6-4a42-81a2-69454acc4e97) +![image](https://github.com/user-attachments/assets/a15fb6fc-4b92-4052-a363-e401955eb732) + +- ๋žŒ๋‹ค์‹์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/cba81268-7ec0-4807-8786-338248ae1ca7) +![image](https://github.com/user-attachments/assets/216c4b3d-ed05-454f-9e58-fb0644bc6c8b) + +###์ŠคํŠธ๋ฆผ์ด๋ž€? +- ์—ฌ๋Ÿฌ ์ž๋ฃŒ์˜ ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด ๋†“์€ ํด๋ž˜์Šค + +- ์ •์ˆ˜ ๋ฐฐ์—ด์—์„œ ์ŠคํŠธ๋ฆผ ํ™œ์šฉ +![image](https://github.com/user-attachments/assets/b9cc9363-6aac-4443-a5c7-f1b5326d5e7d) + +- ArrayList์—์„œ ์ŠคํŠธ๋ฆผ ํ™œ์šฉํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/0806432c-29b1-4efd-a0dd-827f67d64bfb) +![image](https://github.com/user-attachments/assets/45681a86-0a21-4584-b460-b5e6051b88d7) + +- ์ŠคํŠธ๋ฆผ์˜ ํŠน์ง• + 1. ์ž๋ฃŒ์˜ ๋Œ€์ƒ๊ณผ ๊ด€๊ณ„์—†์ด ๋™์ผํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. + 2. ํ•œ ๋ฒˆ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•œ ์ŠคํŠธ๋ฆผ์€ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. + 3. ์ŠคํŠธ๋ฆผ์˜ ์—ฐ์‚ฐ์€ ๊ธฐ์กด ์ž๋ฃŒ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค. + 4. ์ŠคํŠธ๋ฆผ์˜ ์—ฐ์‚ฐ์€ ์ค‘๊ฐ„ ์—ฐ์‚ฐ๊ณผ ์ตœ์ข… ์—ฐ์‚ฐ์ด ์žˆ๋‹ค. + +- reduce() ์—ฐ์‚ฐ + - ๋‚ด๋ถ€์ ์œผ๋กœ ์ŠคํŠธ๋ฆผ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ์†Œ๋ชจํ•˜๋ฉด์„œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ ์ง€์ •ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ +![image](https://github.com/user-attachments/assets/51ecfb0c-d276-4f32-a58b-f424c29a0c68) +![image](https://github.com/user-attachments/assets/dfa8ebee-df73-4bce-94e0-f9750b62ff75) + + + + diff --git "a/\354\244\200\355\231\230/chapter_14.md" "b/\354\244\200\355\231\230/chapter_14.md" new file mode 100644 index 0000000..da8fd5f --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_14.md" @@ -0,0 +1,108 @@ + +# 14 ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ์˜ˆ์™ธ ํด๋ž˜์Šค +2. ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ +3. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ +4. ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ +## ๐Ÿง  ๋ฐฐ์šด ์  +###์˜ค๋ฅ˜๋ž€? +- ํ”„๋กœ๊ทธ๋žจ ์˜ค๋ฅ˜๋Š” 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. + - ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ + - ์‹คํ–‰ ์˜ค๋ฅ˜ + +- ์‹คํ–‰ ์˜ค๋ฅ˜ + - ์‹œ์Šคํ…œ ์˜ค๋ฅ˜ + - ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋™์  ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ์Šคํƒ ๋ฉ”๋ชจ๋ฆฌ์˜ ์˜ค๋ฒ„ํ”Œ๋กœ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ + - ์˜ˆ์™ธ + - ํŒŒ์ผ์„ ์ฝ์–ด ์‚ฌ์šฉํ•˜๋ ค๋Š”๋ฐ ํŒŒ์ผ์ด ์—†๋Š”๊ฒฝ์šฐ, ๋„คํŠธ์›Œํฌ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋ ค๋Š”๋ฐ ์—ฐ๊ฒฐ์ด ์•ˆ๋œ ๊ฒฝ์šฐ +- ์˜ค๋ฅ˜ํด๋ž˜์Šค + - ๋ชจ๋‘ Throwable ํด๋ž˜์Šค์—์„œ ์ƒ์†์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. Error ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ์‹œ์Šคํ…œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ฅผ ๋‹ค๋ฃจ๋ฉฐ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ œ์–ดํ•˜์ง€ ์•Š๋Š”๋‹ค. + - ํ”„๋กœ๊ทธ๋žจ ์—์„œ ์ œ์–ดํ•˜๋Š” ๋ถ€๋ถ„์€ Exception ํด๋ž˜์Šค์™€ ๊ทธ ํ•˜์œ„์— ์žˆ๋Š” ์˜ˆ์™ธ ํด๋ž˜์Šค ์ด๋‹ค. + + - try-catch๋ฌธ + ``` + try{ + ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ๋ถ€๋ถ„ + }catch(์ฒ˜๋ฆฌํ•  ์˜ˆ์™ธ ํƒ€์ž… e) { + try ๋ธ”๋ก ์•ˆ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„ + } + ``` + ![image](https://github.com/user-attachments/assets/2f98e6a7-508a-4d90-8258-1708a2773260) + ![image](https://github.com/user-attachments/assets/53fd3c1c-bbb1-488c-880c-2fcf50d7ff5f) + + ![image](https://github.com/user-attachments/assets/b81d5c39-2dfb-4edd-b67c-888ec678c774) + ![image](https://github.com/user-attachments/assets/6efe9b21-c45a-479b-9640-831352d4adcf) + + - try-catch-finally + ``` + try{ + ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ๋ถ€๋ถ„ + }catch(์ฒ˜๋ฆฌํ•  ์˜ˆ์™ธ ํƒ€์ž… e) { + ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„ + } finally { + ํ•ญ์ƒ ์ˆ˜ํ–‰๋˜๋Š” ๋ถ€๋ถ„ + } + ``` + - finally ๋ธ”๋ก ์‚ฌ์šฉํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/32e116c1-08bc-42fd-863d-fd643e2ad366) + ![image](https://github.com/user-attachments/assets/bbb09efe-691e-4a25-8024-aa6b5b313e4c) + + - try-with-resources + + - AutoCloseable ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ + ![image](https://github.com/user-attachments/assets/c80db23d-b1a0-43b3-b5c3-f7087c58305e) + + + - try-with-resources ์‚ฌ์šฉํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/d1a1b11e-f3ad-4fdf-9b05-31a463152202) + ![image](https://github.com/user-attachments/assets/d6262fe9-fb3f-4490-868c-2a141d6a702c) + + + - throws๋กœ ์˜ˆ์™ธ ๋ฏธ๋ฃจ๊ธฐ + ![image](https://github.com/user-attachments/assets/29d851f8-699f-4e0c-8ea7-3e29ab338cc1) + + + - ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ + ![image](https://github.com/user-attachments/assets/d18c1639-7539-40f6-bb9b-832065e59c47) + ``` + package chapter14.exception; + + public class IDFormatTest { + private String userID; + + public String getUserID() { + return userID; + } + + public void setUserID(String userID) throws IDFormatException { + if (userID == null) { + throw new IDFormatException("์•„์ด๋””๋Š” null์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค"); + } else if (userID.length() < 8 || userID.length() > 20) { + throw new IDFormatException("์•„์ด๋””๋Š” 8์ž ์ด์ƒ 20์ž ์ดํ•˜๋กœ ์“ฐ์„ธ์š”"); + } + this.userID = userID; + } + + public static void main(String[] args) { + IDFormatTest test = new IDFormatTest(); + + String userID = null; + try { + test.setUserID(userID); + } catch (IDFormatException e) { + System.out.println(e.getMessage()); + } + + userID = "1234567"; + try { + test.setUserID(userID); + } catch (IDFormatException e) { + System.out.println(e.getMessage()); + } + } + } + ``` + ![image](https://github.com/user-attachments/assets/44c61179-dbd1-41d7-bba2-3fa94e13adf6) + + diff --git "a/\354\244\200\355\231\230/chapter_15.md" "b/\354\244\200\355\231\230/chapter_15.md" new file mode 100644 index 0000000..f163e58 --- /dev/null +++ "b/\354\244\200\355\231\230/chapter_15.md" @@ -0,0 +1,528 @@ + +# 14 ์ž๋ฐ” ์ž…์ถœ๋ ฅ +## ๐ŸŒ… ๊ณต๋ถ€๊ธฐ๋ก +1. ์ž๋ฐ” ์ž…์ถœ๋ ฅ๊ณผ ์ŠคํŠธ๋ฆผ +2. ํ‘œ์ค€ ์ž…์ถœ๋ ฅ +3. ๋ฐ”์ดํŠธ ๋‹จ์œ„ ์ŠคํŠธ๋ฆผ +4. ๋ฌธ์ž ๋‹จ์œ„ ์ŠคํŠธ๋ฆผ +5. ๋ณด์กฐ ์ŠคํŠธ๋ฆผ +6. ์ง๋ ฌํ™” +7. ๊ทธ ์™ธ ์ž…์ถœ๋ ฅ ํด๋ž˜์Šค +## ๐Ÿง  ๋ฐฐ์šด ์  +### ์ŠคํŠธ๋ฆผ์ด๋ž€? +- ์ž…์ถœ๋ ฅ ์žฅ์น˜์™€ ๋ฌด๊ด€ํ•˜๊ณ  ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ผ์ข…์˜ ๊ฐ€์ƒ ํ†ต๋กœ์ธ ์ŠคํŠธ๋ฆผ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ +- ์ž๋ฃŒ๋ฅผ ์ฝ์–ด ๋“ค์ด๋ ค๋Š” ์†Œ์Šค ์™€ ์ž๋ฃŒ๋ฅผ ์“ฐ๋ ค๋Š” ๋Œ€์ƒ์— ๋”ฐ๋ผ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ŠคํŠธ๋ฆผ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. + +- ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ๊ณผ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ + - ์ž…๋ ฅ ์ž๋ฃŒ์˜ ์ด๋™์ด ์ถœ๋ ฅ ์ž๋ฃŒ์˜ ์ด๋™๊ณผ ํ•œ ์ŠคํŠธ๋ฆผ์—์„œ ๋™์‹œ์— ์ผ์–ด๋‚  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—(๋‹จ๋ฐฉํ–ฅ) + - ์–ด๋–ค ์ŠคํŠธ๋ฆผ์ด ์žˆ๋‹ค๊ณ  ํ•˜๋ฉด ๊ทธ ์ŠคํŠธ๋ฆผ์€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์ด๊ฑฐ๋‚˜ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ์ž…๋‹ˆ๋‹ค + + +- ๋ฐ”์ดํŠธ ๋‹จ์œ„ ์ŠคํŠธ๋ฆผ๊ณผ ๋ฌธ์ž ์ŠคํŠธ๋ฆผ + - ์ž๋ฐ”์˜ ์ŠคํŠธ๋ฆผ์€ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ์ž…์ถœ๋ ฅ์ด ์ด๋ฃจ์–ด์ง„๋‹ค. + - ๋ผ์ง€๋งŒ ๋ฌธ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” charํ˜• 2๋ฐ”์ดํŠธ์ด๊ธฐ ๋•Œ๋ฌธ์— 1๋ฐ”์ดํŠธ๋งŒ ์ฝ์œผ๋ฉด ํ•œ๊ธ€ ๊ฐ™์€ ๋ฌธ์ž๋Š” ๊นจ์ง„๋‹ค + - ์ž…์ถœ๋ ฅ ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ์ธ ๋ฌธ์ž๋ฅผ ์œ„ํ•ด ๋ฌธ์ž ์ŠคํŠธ๋ฆผ์„ ๋ณ„๋„๋กœ ์ œ๊ณต + +- ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ๊ณผ ๋ณด์กฐ ์ŠคํŠธ๋ฆผ + - ๊ธฐ๋ฐ˜ ์ŠคํŠธ๋ฆผ + - ์ฝ์–ด ๋“ค์ผ ๊ณณ์ด๋‚˜ ์จ์•ผ ํ•  ๊ณณ์—์„œ ์ง์ ‘ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž…์ถœ๋ ฅ ๋Œ€์ƒ์— ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์ƒ์„ฑ๋˜๋Š” ์ŠคํŠธ๋ฆผ + - ๋ณด์กฐ ์ŠคํŠธ๋ฆผ + - ์ง์ ‘ ์ฝ๊ณ  ์“ฐ๋Š” ๊ธฐ๋Šฅ์€ ์—†์Œ ๋”ฐ๋ผ์„œ ํ•ญ์ƒ ๋‹ค๋ฅธ ์ŠคํŠธ๋ฆผ์„ ํฌํ•จํ•˜์—ฌ ์ƒ์„ฑ ๋œ๋‹ค. + + +- ํ‘œ์ค€ ์ž…์ถœ๋ ฅ + +- ๋ฌธ์ž ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ +![image](https://github.com/user-attachments/assets/a1decf3c-ca06-43b1-a5d3-24fa9737ad54) +![image](https://github.com/user-attachments/assets/f18eba71-3309-4271-8677-d36be935733e) + +- ๋ฌธ์ž ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ธฐ +![image](https://github.com/user-attachments/assets/4d4d153c-1c17-4bca-ab2c-19e133b7f563) +![image](https://github.com/user-attachments/assets/f673f57b-f9ed-49ae-a6eb-0fcffe19209b) + +- Scanner ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/cf0d1b07-166f-47f0-b3e2-e86f57161489) +![image](https://github.com/user-attachments/assets/22fc1630-e024-4bb2-8982-5fce083e7966) + +- Console ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/5c1f5718-0e98-4569-b0dc-ebc777f3c488) +![image](https://github.com/user-attachments/assets/57fc307f-6b3a-4231-a54d-debe408eb78b) + +- FileInputStream ์‚ฌ์šฉํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/f0c5fe00-a471-480c-b450-a48e4c584f5a) +![image](https://github.com/user-attachments/assets/73055784-c910-45c0-a1f6-d4aadbfb0731) + +- ํŒŒ์ผ ๋๊นŒ์ง€ ์ฝ๊ธฐ +![image](https://github.com/user-attachments/assets/50a91e98-2483-46d8-9ed5-3fb4727cad69) +![image](https://github.com/user-attachments/assets/9faea9ec-119e-42db-8a8b-49c7d0d69b92) + +- byte ๋ฐฐ์—ด๋กœ ์ฝ๊ธฐ +![image](https://github.com/user-attachments/assets/5c907cfc-f2db-4454-b451-1ad9c8306ae8) +![image](https://github.com/user-attachments/assets/f4ef282f-32f6-46ca-8cda-8a92c26ba21e) + +- ํŒŒ์ผ์— ํ•œ ๋ฐ”์ดํŠธ์”ฉ ์ถœ๋ ฅํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/746e0957-386f-4890-bad0-9269093111c6) +![image](https://github.com/user-attachments/assets/dac7c117-9458-452c-bc5a-e133b590e17b) +![image](https://github.com/user-attachments/assets/389de19e-4457-4a98-a82c-23d0780a72aa) + +- ํŒŒ์ผ์— ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/0ef5be67-cc1f-4263-8081-be5766df9178) +![image](https://github.com/user-attachments/assets/02fbe494-4d47-48b1-9af8-269170a4fb54) +![image](https://github.com/user-attachments/assets/a652cef7-24ce-4e55-b7a8-2126296beb49) + +- ํŒŒ์ผ์— ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/4c656b77-1020-4078-9e8d-b9c38ccabe46) +![image](https://github.com/user-attachments/assets/4c2b4501-3bd9-47f3-aa8b-80df9c606f66) +![image](https://github.com/user-attachments/assets/dfdf1eb8-9b1c-4e7c-87b6-d56ccc46008f) + +- FileReader๋กœ ์ฝ๊ธฐ +![image](https://github.com/user-attachments/assets/5fad2c66-48a9-4c9d-9182-1b96540eb512) +![image](https://github.com/user-attachments/assets/8ca83561-7626-403b-a079-5456e8a36e04) + +- FileWriter๋กœ ์“ฐ๊ธฐ +![image](https://github.com/user-attachments/assets/8039b272-06eb-42d0-8c1d-62d0843648df) +![image](https://github.com/user-attachments/assets/685e8364-aa5b-413c-8e51-b4efb81eb7db) +![image](https://github.com/user-attachments/assets/9214bb68-4a6e-4509-847e-645f2435bcd0) + +- InputStreamReader ์‚ฌ์šฉํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/b6f46926-065f-4274-ac16-b9ac6e683dfb) +![image](https://github.com/user-attachments/assets/4a4f588a-e707-45d0-959f-597aed8d8db1) + +- ํŒŒ์ผ ๋ณต์‚ฌํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/6fff3198-9731-4b0f-a11c-61c1e2d495d7) + +- ๋ฒ„ํผ๋ง ๊ธฐ๋Šฅ์œผ๋กœ ํŒŒ์ผ ๋ณต์‚ฌํ•˜๊ธฐ +![image](https://github.com/user-attachments/assets/c1da8d50-b228-4f2e-a56e-98c5650d1ed3) + +- DataInputStream / DataOutputStream ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ +``` +package chapter15.stream.decorator; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +public class DataStreamTest { + public static void main(String[] args) { + // ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์— ์“ฐ๊ธฐ + try (FileOutputStream fos = new FileOutputStream("/home/alpaca/Java/src/chapter15/data.txt"); + DataOutputStream dos = new DataOutputStream(fos)) { + // ๊ฐ ์ž๋ฃŒํ˜•์— ๋งž๊ฒŒ ์ž๋ฃŒ๋ฅผ ์”€ + dos.writeByte(100); + dos.writeChar('A'); + dos.writeInt(10); + dos.writeFloat(3.14f); + dos.writeUTF("Test"); + + } catch (IOException e) { + e.printStackTrace(); + } + + // ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ + try (FileInputStream fis = new FileInputStream("/home/alpaca/Java/src/chapter15/data.txt"); + DataInputStream dis = new DataInputStream(fis)) { + // ์ž๋ฃŒํ˜•์— ๋งž๊ฒŒ ์ž๋ฃŒ๋ฅผ ์ฝ์–ด ์ถœ๋ ฅํ•จ. ํŒŒ์ผ์— ์“ด ์ˆœ์„œ์™€ ๊ฐ™์€ ์ˆœ์„œ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋กœ ์ฝ์–ด์•ผ ํ•จํ•จ + System.out.println(dis.readByte()); + System.out.println(dis.readChar()); + System.out.println(dis.readInt()); + System.out.println(dis.readFloat()); + System.out.println(dis.readUTF()); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} + +``` +![image](https://github.com/user-attachments/assets/456c15b9-9b2b-4cc5-8512-3b0f485d77b2) + +### ํ•™์  ์‚ฐ์ถœ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ + +- ํ•™์ƒ ํด๋ž˜์Šค ๊ตฌํ˜„ +``` +package project.school; + +import java.util.ArrayList; + + +public class Student { + private int studentId; + private String studentName; + private Subject majorSubject; + + private ArrayList scoreList = new ArrayList(); + + public Student (int studentId, String studentName, Subject majSubject) { + this.studentId = studentId; + this.studentName = studentName; + this.majorSubject = majSubject; + } + + public void addSubjectScore(Score score) { + scoreList.add(score); + } + + public int getStudentId() { + return studentId; + } + + public void setStudentId(int studentId) { + this.studentId = studentId; + } + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public Subject getMajorSubject() { + return majorSubject; + } + + public void setMajorSubject(Subject majorSubject) { + this.majorSubject = majorSubject; + } + + public ArrayList getScoreList() { + return scoreList; + } + + public void setScoreList(ArrayList scoreList) { + this.scoreList = scoreList; + } + + +} +``` + +- ๊ณผ๋ชฉ ํด๋ž˜์Šค ๊ตฌํ˜„ +``` +package project.school; + +import java.util.ArrayList; +import project.school.utils.Define; + + +public class Subject { + private String subjectName; + private int subjectId; + private int gradeType; + + private ArrayList studentList = new ArrayList(); + + public Subject(String subjectName, int subjectId) { + this.subjectName = subjectName; + this.subjectId = subjectId; + this.gradeType = Define.AB_TYPE; + } + + public String getSubjectName() { + return subjectName; + } + + public void getSubjectName(String subjectNaem, String subjectName) { + this.subjectName = subjectName; + } + + public int getSubjectId() { + return subjectId; + } + + public void setSubjectId(int subjectId) { + this.subjectId = subjectId; + } + + public ArrayList getStudentList() { + return studentList; + } + + public void setStudentList(ArrayList studentList) { + this.studentList = studentList; + } + + public int getGradeType() { + return gradeType; + } + + public void setGradeType(int gradeType) { + this.gradeType = gradeType; + } + + public void register(Student student) { + studentList.add(student); + } + +} + +``` + +- ์ ์ˆ˜ ํด๋ž˜์Šค ๊ตฌํ˜„ +``` +package project.school; +public class Score { + int studentId; + Subject subject; + int point; + + public Score(int studentId, Subject subject, int point) { + this.studentId = studentId; + this.subject = subject; + this.point = point; + } + + public int getStudenId() { + return studentId; + } + + public void setStudentId(int studentId) { + this.studentId = studentId; + } + + public Subject getSubject() { + return subject; + } + + public void setSubject(Subject subject) { + this.subject = subject; + } + + public int getPoint() { + return point; + } + + public void setPoint(int point) { + this.point = point; + } + + public String toString() { + return "ํ•™๋ฒˆ:" + studentId + "," + subject.getSubjectName() + ":" + point; + } +} +``` + +- ์ƒ์ˆ˜ ๊ฐ’ ์ •์˜ +![image](https://github.com/user-attachments/assets/f898f1f9-8f49-4d81-8a08-d35936f3ff41) + +- ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ +![image](https://github.com/user-attachments/assets/22f29475-0f71-47ab-8139-f4c482151d0d) + +- ์ผ๋ฐ˜ ๊ณผ๋ชฉ ํ•™์  ์ •์ฑ… ํด๋ž˜์Šค +![image](https://github.com/user-attachments/assets/c054f6a4-ce6f-4629-bbdc-b73d3dab283f) + +- ํ•„์ˆ˜ ๊ณผ๋ชฉ ํ•™์  ์ •์ฑ… ํด๋ž˜์Šค +![image](https://github.com/user-attachments/assets/12a01cab-3223-4efb-b3d4-9db8b4368c29) + +- ๋ฆฌํฌํŠธ ํด๋ž˜์Šค ๊ตฌํ˜„ +``` +package project.school.report; + +import java.util.ArrayList; + +import project.grade.BasicEvaluation; +import project.grade.GradeEvaluation; +import project.grade.MajorEvaluation; +import project.grade.PassFailEvaluation; +import project.school.School; +import project.school.Score; +import project.school.Student; +import project.school.Subject; +import project.school.utils.Define; + +public class GenerateGradeReport { + + School school = School.getInstance(); + + public static final String TITLE = "์ˆ˜๊ฐ•์ƒ ํ•™์  \t\t\n"; + public static final String HEADER = " ์ด๋ฆ„ | ํ•™๋ฒˆ |ํ•„์ˆ˜๊ณผ๋ชฉ | ์ ์ˆ˜ \n"; + public static final String LINE = "-------------------------------------\n"; + + private StringBuffer buffer = new StringBuffer(); + + public String getReport() { + ArrayList subjectList = school.getSubjectList(); + + for (Subject subject : subjectList) { + makeHeader(subject); + makeBody(subject); + makeFooter(); + } + + return buffer.toString(); + } + + public void makeHeader(Subject subject) { + buffer.append(GenerateGradeReport.LINE); + buffer.append("\t" + subject.getSubjectName() + "\n"); + buffer.append(GenerateGradeReport.TITLE); + buffer.append(GenerateGradeReport.HEADER); + buffer.append(GenerateGradeReport.LINE); + } + + public void makeBody(Subject subject) { + ArrayList studentList = subject.getStudentList(); + + for (int i = 0; i < studentList.size(); i++) { + Student student = studentList.get(i); + buffer.append(student.getStudentName()); + buffer.append(" | "); + buffer.append(student.getStudentId()); + buffer.append(" | "); + buffer.append(student.getMajorSubject().getSubjectName()); + buffer.append(" \t"); + + getScoreGrade(student, subject.getSubjectId()); + buffer.append("\n"); + buffer.append(LINE); + } + } + + public void getScoreGrade(Student student, int subjectId) { + ArrayList scoreList = student.getScoreList(); + int majorId = student.getMajorSubject().getSubjectId(); + + GradeEvaluation[] gradeEvaluation = { + new BasicEvaluation(), // ์ผ๋ฐ˜ ๊ณผ๋ชฉ + new MajorEvaluation(), // ํ•„์ˆ˜ ๊ณผ๋ชฉ + new PassFailEvaluation() + }; + + for (int i = 0; i < scoreList.size(); i++) { + Score score = scoreList.get(i); + + if (score.getSubject().getSubjectId() == subjectId) { + String grade; + + if (score.getSubject().getSubjectId() == majorId) { + grade = gradeEvaluation[Define.SAB_TYPE].getGrade(score.getPoint()); + } else { + grade = gradeEvaluation[Define.AB_TYPE].getGrade(score.getPoint()); + } + + buffer.append(score.getPoint()); + buffer.append(":"); + buffer.append(grade); + buffer.append(" "); + } + } + } + + public void makeFooter() { + buffer.append("\n"); + } +} + +``` +- Pass/Fail ํ•™์  ํด๋ž˜์Šค ๊ตฌํ˜„ +![image](https://github.com/user-attachments/assets/8d7b8a69-2792-44b9-a948-4500b44f25ff) + +- ํ•™๊ต ํด๋ž˜์Šค +![image](https://github.com/user-attachments/assets/71680f59-41f7-46b4-9799-ee262eb0c32e) + +- ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค +``` +package project.test; + +import project.school.School; +import project.school.Score; +import project.school.Student; +import project.school.Subject; +import project.school.report.GenerateGradeReport; +import project.school.utils.Define; + +public class TestMain { + + School goodSchool = School.getInstance(); + Subject korean; + Subject math; + Subject dance; + + GenerateGradeReport gradeReport = new GenerateGradeReport(); + + public static void main(String[] args) { + TestMain test = new TestMain(); + + test.createSubject(); + test.createStudent(); + + String report = test.gradeReport.getReport(); // ์„ฑ์  ๊ฒฐ๊ณผ ์ƒ์„ฑ + System.out.println(report); + } + + public void createSubject() { + korean = new Subject("๊ตญ์–ด", Define.KOREAN); + math = new Subject("์ˆ˜ํ•™", Define.MATH); + dance = new Subject("๋ฐฉ์†ก๋Œ„์Šค", Define.DANCE); + + dance.setGradeType(Define.PF_TYPE); + + goodSchool.addSubject(korean); // ํ…Œ์ŠคํŠธ ๊ณผ๋ชฉ ์ƒ์„ฑ + goodSchool.addSubject(math); + goodSchool.addSubject(dance); + } + + public void createStudent() { + Student student1 = new Student(181213, "์•ˆ์„ฑ์ผ", korean); + Student student2 = new Student(181518, "์œคํƒœ์›…", math); + Student student3 = new Student(171230, "์ด์ˆœ์‹ ", korean); + Student student4 = new Student(171255, "์กฐ์Šน์—ฐ", korean); + Student student5 = new Student(171590, "์ตœํƒœ์˜", math); + + goodSchool.addStudent(student1); // goodSchool์— ํ•™์ƒ ์ถ”๊ฐ€ + goodSchool.addStudent(student2); + goodSchool.addStudent(student3); + goodSchool.addStudent(student4); + goodSchool.addStudent(student5); + + korean.register(student1); // ๊ตญ์–ด ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•˜๋Š” ํ•™์ƒ ๋“ฑ๋ก + korean.register(student2); + korean.register(student3); + korean.register(student4); + korean.register(student5); + + math.register(student1); // ์ˆ˜ํ•™ ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•˜๋Š” ํ•™์ƒ ๋“ฑ๋ก + math.register(student2); + math.register(student3); + math.register(student4); + math.register(student5); + + dance.register(student1); + dance.register(student2); + dance.register(student3); + + // ๊ฐ ํ•™์ƒ์˜ ๊ณผ๋ชฉ ์ ์ˆ˜ ์ถ”๊ฐ€ + addScoreForStudent(student1, korean, 95); + addScoreForStudent(student1, math, 56); + addScoreForStudent(student1, dance, 95); + + addScoreForStudent(student2, korean, 95); + addScoreForStudent(student2, math, 95); + addScoreForStudent(student2, dance, 85); + + addScoreForStudent(student3, korean, 100); + addScoreForStudent(student3, math, 88); + addScoreForStudent(student3, dance, 85); + + addScoreForStudent(student4, korean, 89); + addScoreForStudent(student4, math, 95); + + addScoreForStudent(student5, korean, 85); + addScoreForStudent(student5, math, 56); + } + + // ๊ณผ๋ชฉ๋ณ„ ์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ + public void addScoreForStudent(Student student, Subject subject, int point) { + Score score = new Score(student.getStudentId(), subject, point); + student.addSubjectScore(score); + } +} + +``` +![image](https://github.com/user-attachments/assets/afd1535c-0416-4344-ad34-b11eaf64b257) + +