TIL(Today I Learned)_230731

Aspyn Choiยท2023๋…„ 7์›” 31์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
14/37

โ˜๐Ÿป์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ

1.์ปฌ๋ ‰์…˜(Collection)

- ์ปฌ๋ ‰์…˜์ด๋ž€?

: ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋ชจ์Œ(ํ‹€), ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์กด์žฌํ•จ
: *List, set, *map์„ ์•Œ์•„๋ณผ ๊ฒƒ -> ๋ฐฐ์—ด๋ณด๋‹ค ์ง„ํ™”๋œ ํ˜•ํƒœ์ž„

- ์ž๋ฃŒ๊ตฌ์กฐ๋ž€?

: ๋ฐ์ดํ„ฐ(์ž๋ฃŒ)๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ตฌ์กฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก ์ด๋‹ค. -> ์•Œ๊ณ ๋ฆฌ์ฆ˜

- ๋ฐฐ์—ด์˜ ๋ฌธ์ œ์ 

  1. ํ•œ ๋ฒˆ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
    : ๊ณต๊ฐ„ ํฌ๊ธฐ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ๐Ÿกช ํ• ๋‹น ์‹œ ๋„‰๋„‰ํ•œ ํฌ๊ธฐ๋กœ ํ• ๋‹นํ•˜๊ฒŒ ๋จ (๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„)
    : ํ•„์š”์— ๋”ฐ๋ผ ๊ณต๊ฐ„์„ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ผ ์ˆ˜ ์—†์Œ
  2. ๋ฐฐ์—ด์— ๊ธฐ๋ก๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ค‘๊ฐ„ ์œ„์น˜์˜ ์ถ”๊ฐ€, ์‚ญ์ œ๊ฐ€ ๋ถˆํŽธํ•˜๋‹ค.
    : ์ถ”๊ฐ€, ์‚ญ์ œํ•  ๋ฐ์ดํ„ฐ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๊ธฐ๋ก๋œ ๋ฐ์ดํ„ฐ๊นŒ์ง€ ํ•˜๋‚˜์”ฉ ๋’ค๋กœ ๋ฐ€์–ด๋‚ด๊ณ  ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ(๋ณต์žกํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜)
  3. ํ•œ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค.

- ์ปฌ๋ ‰์…˜์˜ ์žฅ์ 

  1. ์ €์žฅํ•˜๋Š” ํฌ๊ธฐ์˜ ์ œ์•ฝ์ด ์—†๋‹ค.(๋”๋„ฃ์–ด๋„ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚˜๊ณ , ๋œ๋„ฃ์–ด๋„ ๊ธธ์ด๊ฐ€ ์ค„์–ด๋“ ๋‹ค)
  2. ์ถ”๊ฐ€, ์‚ญ์ œ, ์ •๋ ฌ ๋“ฑ์˜ ๊ธฐ๋Šฅ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ๋œ๋‹ค.
    : ์ž๋ฃŒ๋ฅผ ๊ตฌ์กฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌ ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋‚ด์žฅ๋˜์–ด ์žˆ์–ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ด ํ•„์š” ์—†์Œ
  3. ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค.
    : ๊ฐ์ฒด๋งŒ ์ €์žฅ({Student(), Employee(), integer, int arr[4]}๋“ฑ ์œผ๋กœ)ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”์— ๋”ฐ๋ผ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ์ €์žฅํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ Wrapperํด๋ž˜์Šค ์‚ฌ์šฉ.
    : Wrapper ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉํ•˜๋Š” 8๊ฐ€์ง€ -> Bolean, Character, Byte, Short, Integer, Long, Float, Double

- ์ปฌ๋ ‰์…˜์˜ ์ฃผ์š” ์ธํ„ฐํŽ˜์ด์Šค

  1. ์ธํ„ฐํŽ˜์ด์Šค
    : ๊ด€๋ จ์—†๋Š” ๊ฒƒ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ
    : ๊ณตํ†ต๋œ ๋ฉ”์†Œ๋“œ๋ช…์„ ์ œ๊ณตํ•˜์—ฌ ๊ทœ์—ญ์„ ๋งŒ๋“  ๊ฒƒ

: Collection ์ธํ„ฐํŽ˜์ด์Šค -> List, Set์˜ ๊ณตํ†ต ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๋ถ€๋ชจ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.
: Java Collection -> ์ž๋ฐ”์˜ ์ž๋ฃŒ๊ตฌ์กฐ ๋ชจ์Œ(List, Set, Map)
3. List(๋‚˜์—ด) -> ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด ๋ชจ์–‘์˜ ๊ตฌ์กฐ์ด๋ฉฐ, ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋‹ค(์ค‘๋ณต๋ฐ์ดํ„ฐ ํ—ˆ์šฉ๋จ).
4. Set(์ง‘ํ•ฉ) -> ์ฃผ๋จธ๋‹ˆ ๋ชจ์–‘์˜ ์ง‘ํ•ฉ, ๊ธฐ๋ณธ์ ์œผ๋กœ ์ค‘๋ณต๋ฐ์ดํ„ฐ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
5. Map -> Key : Value๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ ์žฌ๋จ. Key๋Š” ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€์•Š๊ณ , Value๋Š” ์ค‘๋ณต์ด ํ—ˆ์šฉ๋จ

2. List

- List๋ž€?

: ์ž๋ฃŒ๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‚˜์—ดํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์ธ๋ฑ์Šค๋กœ ๊ด€๋ฆฌ๋˜๋ฉฐ, ์ค‘๋ณตํ•ด์„œ ๊ฐ์ฒด ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ ArrayList์™€ Vector, LinkedList๊ฐ€ ์žˆ์Œ

- List ๊ณ„์—ด ์ฃผ์š” ๋ฉ”์†Œ๋“œ

- List ์˜ˆ์ œ

โœ… Student ํด๋ž˜์Šค

package edu.kh.collection.model.vo;

public class Student {

	// ํ•„๋“œ
	private String name;
	private int age;
	private String region;
	private char gender;
	private int score;
	
	public Student() {}

	public Student(String name, int age, String region, char gender, int score) {
		super();
		this.name = name;
		this.age = age;
		this.region = region;
		this.gender = gender;
		this.score = score;
	}

	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;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	

	//toString()์˜ค๋ฒ„๋ผ์ด๋”ฉ ์ž๋™์™„์„ฑ
	// alt + shifg + s -> s
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", region=" + region + ", gender=" + gender + ", score="
				+ score + "]";
	}
	
	
}

โœ… StudentService ํด๋ž˜์Šค

package edu.kh.collection.model.service;

//import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import edu.kh.collection.model.vo.Student;

public class StudentService {
	
	// ํ•„๋“œ
	private Scanner sc = new Scanner(System.in);
	
	// ํ•™์ƒ ์ •๋ณด๋ฅผ ์ €์žฅํ•  List ์ƒ์„ฑ
	
	// java.util.List ์ธํ„ฐํŽ˜์ด์Šค : List์— ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ํ•„์ˆ˜ ๊ธฐ๋Šฅ์„ ๋ชจ์•„๋‘” ์ธํ„ฐํŽ˜์ด์Šค
	// * ์ธํ„ฐํŽ˜์ด์Šค ๊ฐ์ฒด ์ƒ์„ฑ x, ๋ถ€๋ชจ ์ฐธ์กฐ๋ณ€์ˆ˜ ใ…‡

	// java.util.ArrayList : ๋ฐฐ์—ด ํ˜•ํƒœ List(๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ List ์ž์‹ ํด๋ž˜์Šค)
	
	// ArrayList() ๊ธฐ๋ณธ ์ƒ์„ฑ์ž : ๊ธฐ๋ณธ ํฌ๊ธฐ 10์งœ๋ฆฌ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
	//							-> ํ•˜์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋Š” ํฌ๊ธฐ๊ฐ€ ๋Š˜์—ˆ๋‹ค ์ค„์—ˆ๋‹ค ํ•˜๊ธฐ๋•Œ๋ฌธ์— ํฐ ์˜๋ฏธ๋Š” ์—†๋‹ค.
	
	// ArrayList(์šฉ๋Ÿ‰) : ์šฉ๋Ÿ‰๋งŒํผ์˜ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ -> ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํฐ ์˜๋ฏธ๋Š” ์—†๋‹ค.
	//					-> ๋„ˆ๋ฌด ํฐ๊ฐ’์„ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์†Œ๋ชจํ•จ.
	
	//private List<Student> studentList = new ArrayList<Student>(); // ๊ฒ€์ƒ‰, ์กฐํšŒ์— ํšจ์œจ์ 
	private List<Student> studentList = new LinkedList<Student>(); // ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ์— ํšจ์œจ์ 
	// ์ €์žฅ๋˜๋Š” ํƒ€์ž…์ด Student๋กœ ์ œํ•œ๋œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค๋Š” ๋œป
	// == Student๋งŒ ์ €์žฅ ๊ฐ€๋Šฅ == ๋ชจ๋“ ๊ฒŒ Student
	// == Student์ž„์„ ๊ฒ€์‚ฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
	
	public StudentService() {
		
		studentList.add( new Student("ํ™๊ธธ๋™", 25, "์„œ์šธ์‹œ ์ค‘๊ตฌ", 'M', 90) ) ;
		studentList.add( new Student("๊ณ ์˜ํฌ", 23, "๊ฒฝ๊ธฐ๋„ ์•ˆ์‚ฐ์‹œ", 'F', 100) ) ;
		studentList.add( new Student("๊ฐ•์•„์ง€", 30, "์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ", 'M', 80) ) ;
		studentList.add( new Student("์˜ค๋ฏธ๋‚˜", 27, "์ถฉ๋ถ ์ฒญ์ฃผ์‹œ", 'F', 90) ) ;
		studentList.add( new Student("๋ฐ•์ฃผํฌ", 24, "์„œ์šธ์‹œ ์„œ๋Œ€๋ฌธ๊ตฌ", 'F', 70) ) ;
		
	}
	
	
	
	
	public void ex() {
		// List ํ…Œ์ŠคํŠธ
		
		// List.add(Object e) : ๋ฆฌ์ŠคํŠธ์— ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€
		// * ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์ด Object == ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Œ
		
		studentList.add(new Student()); // 0๋ฒˆ ์ธ๋ฑ์Šค
		//studentList.add(sc); // 1๋ฒˆ ์ธ๋ฑ์Šค
		//studentList.add("๋ฌธ์ž์—ด"); // 2๋ฒˆ ์ธ๋ฑ์Šค
		//studentList.add(new Object()); // 3๋ฒˆ ์ธ๋ฑ์Šค
		// -> ์ปฌ๋ ‰์…˜์˜ ํŠน์ง• : ์—ฌ๋Ÿฌ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
		
		// (๋ฐ˜ํ™˜ํ˜•)
		// Object List.get(index i) : ๋ฆฌ์ŠคํŠธ์—์„œ i๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
		// ๋ฐ˜ํ™˜ํ˜•์ด Object == ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
		
		if( studentList.get(0) instanceof Student) {
			System.out.println(((Student)studentList.get(0)).getName());
		}
		
		// ์ œ๋„ค๋ฆญ์Šค(Generics) == ์ œ๋„ค๋ฆญ
		// -> ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋˜๋Š” ๊ฐ์ฒด ํƒ€์ž…์„ ํ•œ๊ฐ€์ง€๋กœ ์ œํ•œ
		System.out.println(studentList.get(0).getName());
		
	}

	
	/** 
	 * ๋ฉ”๋‰ด ์ถœ๋ ฅ์šฉ ๋ฉ”์„œ๋“œ

	 * alt + shift + j 
	 * ๋ฉ”์„œ๋“œ ์„ค๋ช…์šฉ ์ฃผ์„
	 * @author cje@naver.com.
	 */
	public void displayMenu() {
		
		int menuNum = 0;
		
		do {
			
			System.out.println("\n===========ํ•™์ƒ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ===========\n");
			System.out.println("1. ํ•™์ƒ ์ •๋ณด ์ถ”๊ฐ€");
			System.out.println("2. ํ•™์ƒ ์ „์ฒด ์กฐํšŒ");
			System.out.println("3. ํ•™์ƒ ์ •๋ณด ์ˆ˜์ •");
			System.out.println("4. ํ•™์ƒ ์ •๋ณด ์ œ๊ฑฐ");
			System.out.println("5. ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰(์ผ์น˜)");
			System.out.println("6. ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰(ํฌํ•จ)");
			
			System.out.println("0. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
			
			System.out.print("\n๋ฉ”๋‰ด ๋ฒˆํ˜ธ ์„ ํƒ >> ");
			
			try {
				
				menuNum = sc.nextInt();
				System.out.println();
				
				switch(menuNum) {
				case 1 : System.out.println(addStudent()); break;
				case 2 : selectAll(); break;
				case 3 : System.out.println(updateStudent()); break;
				case 4 : System.out.println(removeStudent()); break;
				case 5 : searchName(); break;
				case 6 : searchName2(); break;
				case 0 : System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ..."); break;
				default : System.out.println("๋ฉ”๋‰ด์— ์ž‘์„ฑ๋œ ๋ฒˆํ˜ธ๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
				
				}
				
			} catch(InputMismatchException e) {
				System.out.println("error : ์ž…๋ ฅ ํ˜•์‹์ด ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.");
				 
				sc.nextLine(); // ์ž…๋ ฅ๋ฒ„ํผ์— ๋‚จ์•„์žˆ๋Š” ์ž˜๋ชป๋œ ๋ฌธ์ž์—ด ์ œ๊ฑฐ
				
				menuNum = -1; // ์ฒซ ๋ฐ˜๋ณต ์‹œ ์ž˜๋ชป ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ menuNum์ด 0์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ์ข…๋ฃŒ๋˜๋Š”๋ฐ,
				// ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ์œ„ํ•ด ์ž„์˜๊ฐ’ -1 ๋Œ€์ž…
			}
			
		}while(menuNum != 0);
		
	}
	
	
	/**
	 * 1. ํ•™์ƒ ์ •๋ณด ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ
	 * - ์ถ”๊ฐ€ ์„ฑ๊ณต ์‹œ "์„ฑ๊ณต", ์‹คํŒจ ์‹œ "์‹คํŒจ"๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
	 */
	public String addStudent() throws InputMismatchException{
		System.out.println("==============ํ•™์ƒ ์ •๋ณด ์ถ”๊ฐ€==============");
		
		System.out.print("์ด๋ฆ„ : ");
		String name = sc.next();
		
		System.out.print("๋‚˜์ด : ");
		int age = sc.nextInt();
		sc.nextLine(); // ์ž…๋ ฅ๋ฒ„ํผ ๊ฐœํ–‰ ๋ฌธ์ž ์ œ๊ฑฐ
		
		System.out.print("์‚ฌ๋Š”๊ณณ : ");
		String region = sc.nextLine();
		
		System.out.print("์„ฑ๋ณ„(M/F) : ");
		char gender = sc.next().charAt(0);
		
		System.out.print("์ ์ˆ˜ : ");
		int score = sc.nextInt();
		
		// Student ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ List์— ์ถ”๊ฐ€
		
		if(studentList.add( new Student(name, age, region, gender, score) ) ) {
			// add์˜ ๋ฐ˜ํ™˜ํ˜•์€ boolean ํƒ€์ž…
			// ์ œ๋„ค๋ฆญ <Student>๋•Œ๋ฌธ์— Listํƒ€์ž…์ด Student๋กœ ์ œํ•œ๋จ
			
			return "์„ฑ๊ณต";
		}else {
			return "์‹คํŒจ";
		}
		
	}
	
	/**
	 *  2. ํ•™์ƒ ์ „์ฒด ์กฐํšŒ ๋ฉ”์„œ๋“œ
	 */
	public void selectAll() {
		
		// - List๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋‹ค.(0๋ฒˆ ๋ถ€ํ„ฐ ์‹œ์ž‘)
		// - List์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์–ป์–ด์˜ค๋Š” ๋ฐฉ๋ฒ• : int List.size()
		// -> ๋ฐฐ์—ด๋ช….length ๋Œ€์‹  ์‚ฌ์šฉ
		
		// - List๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•
		// 	boolean List.isEmpty() : ๋น„์–ด์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜
		
		System.out.println("========ํ•™์ƒ ์ „์ฒด ์กฐํšŒ========");
		
		// studentList๊ฐ€ ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ "ํ•™์ƒ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค" ์ถœ๋ ฅ
		
		//studentList.size() == 0 ๋˜๋Š”
		if(studentList.isEmpty()) {
			System.out.println("ํ•™์ƒ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
			 
			return; // ํ˜„์žฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ๋Œ์•„๊ฐ
					// ๋‹จ, ๋ฐ˜ํ™˜๊ฐ’์€ ์—†๋‹ค(void)
		}
		
		// ์ผ๋ฐ˜ for๋ฌธ
//		for(int i = 0; i < studentList.size(); i++) {
//			System.out.println(studentList.get(i));
//		}
		
		// ํ–ฅ์ƒ๋œ for๋ฌธ(for each๋ฌธ)
		// - ์ปฌ๋ ‰์…˜, ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ˜๋ณต์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์€ for๋ฌธ
		// (์ˆœ์ฐจ์  : 0๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰์š”์†Œ๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ 1์”ฉ ์ฆ๊ฐ€)
		
		// [์ž‘์„ฑ๋ฒ•]
		// for(์ปฌ๋ ‰์…˜ ๋˜๋Š” ๋ฐฐ์—ด์—์„œ ๊บผ๋‚ธ ํ•œ๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ : ์ปฌ๋ ‰์…˜๋ช… ๋˜๋Š” ๋ฐฐ์—ด๋ช…){  }
		int index = 0;
		for( Student std : studentList ) {
			// std์—๋Š” for๋ฌธ ๋ฐ˜๋ณต ์‹œ ๋งˆ๋‹ค 0,1,2... ์ธ๋ฑ์Šค ์š”์†Œ๋“ค์„ ํ•œ๋ฒˆ์”ฉ ์ €์žฅ๋จ
			System.out.print((index++)+ "๋ฒˆ : ");
			System.out.println(std);
		}
		
	}

	
	/**
	 * 3. ํ•™์ƒ ์ •๋ณด ์ˆ˜์ • ๋ฉ”์„œ๋“œ
	 */
	public String updateStudent() throws InputMismatchException{
		
		// Student List.set(int index, Student e) 
		// -> List์˜ i๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ „๋‹ฌ๋ฐ›์€ e๋กœ ๋ณ€๊ฒฝ
		// -> (๋ฐ˜ํ™˜) Student == ๋ณ€๊ฒฝ ์ „ Student ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค.
		
		System.out.println("==========ํ•™์ƒ ์ •๋ณด ์ˆ˜์ •==========");
		
		System.out.print("์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ : ");
		int index = sc.nextInt();
		
		// 1) ํ•™์ƒ ์ •๋ณด๊ฐ€ studentList์— ์žˆ๋Š”๊ฐ€? == ๋น„์–ด์žˆ๋Š”๊ฐ€
		if(studentList.isEmpty()) {
			return "์ž…๋ ฅ๋œ ํ•™์ƒ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.";

			// 2) ์ž…๋ ฅ๋œ ์ˆซ์ž๊ฐ€ 0๋ณด๋‹ค ์ž‘์€๊ฐ€? (์Œ์ˆ˜ ๊ฒ€์‚ฌ)
		}else if(index < 0) {
			return "์Œ์ˆ˜๋Š” ์ž…๋ ฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค";
			
			// 3) ๋งŒ์•ฝ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ -> ์˜ˆ์™ธ์ฒ˜๋ฆฌ throws
		
		// 4) ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๊ฐ€ studentList ๋ฒ”์œ„ ๋‚ด ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์ธ๊ฐ€?	
		}else if(index >= studentList.size()) {
			return "๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„  ๊ฐ’์„ ์ž…๋ ฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.";
		
		}else {
			// ์ˆ˜์ • ์ฝ”๋“œ ์ž‘์„ฑ
			System.out.println(index + "๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์ €์žฅ๋œ ํ•™์ƒ ์ •๋ณด");
			System.out.println(studentList.get(index));
			
			System.out.print("์ด๋ฆ„ : ");
			String name = sc.next();
			
			System.out.print("๋‚˜์ด : ");
			int age = sc.nextInt();
			sc.nextLine(); // ์ž…๋ ฅ๋ฒ„ํผ ๊ฐœํ–‰ ๋ฌธ์ž ์ œ๊ฑฐ
			
			System.out.print("์‚ฌ๋Š”๊ณณ : ");
			String region = sc.nextLine();
			
			System.out.print("์„ฑ๋ณ„(M/F) : ");
			char gender = sc.next().charAt(0);
			
			System.out.print("์ ์ˆ˜ : ");
			int score = sc.nextInt();
			
			// ์ž…๋ ฅ๋ฐ›์€ index ๋ฒˆ์งธ์— ์ƒˆ๋กœ์šด ํ•™์ƒ ์ •๋ณด๋ฅผ ์…‹ํŒ… -> ์ˆ˜์ •
			// ์ด๋•Œ, index๋ฒˆ์งธ์— ์žˆ๋˜ ๊ธฐ์กด ํ•™์ƒ ์ •๋ณด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
			Student temp = studentList.set(index, new Student(name, age, region, gender, score));
			
			return temp.getName() + "์˜ ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.";
			
		}
		
	}
	
	
	/**
	 * 4. ํ•™์ƒ ์ •๋ณด ์ œ๊ฑฐ ๋ฉ”์„œ๋“œ
	 * @throws InputMismatchException
	 */
	public String removeStudent() throws InputMismatchException{
		
		// Student List.remove(int index)
		// ๋ฆฌ์ŠคํŠธ์—์„œ index๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐ
		// (๋ฐ˜ํ™˜) ์ œ๊ฑฐ๋œ index๋ฒˆ์˜ ์š”์†Œ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
		// *List๋Š” ์ค‘๊ฐ„์— ๋น„์–ด์žˆ๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์—†๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ
		// remove() ๋™์ž‘ ์‹œ ๋’ค์ชฝ ์š”์†Œ๋ฅผ ํ•œ ์นธ์”ฉ ๋‹น๊ฒจ์˜จ๋‹ค.
		
		System.out.println("==========ํ•™์ƒ ์ •๋ณด ์ œ๊ฑฐ==========");
		
		System.out.print("์ธ๋ฑ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ : ");
		int index = sc.nextInt();
		
		// 1) ํ•™์ƒ ์ •๋ณด๊ฐ€ studentList์— ์žˆ๋Š”๊ฐ€? == ๋น„์–ด์žˆ๋Š”๊ฐ€
		if(studentList.isEmpty()) {
			return "์ž…๋ ฅ๋œ ํ•™์ƒ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.";

			// 2) ์ž…๋ ฅ๋œ ์ˆซ์ž๊ฐ€ 0๋ณด๋‹ค ์ž‘์€๊ฐ€? (์Œ์ˆ˜ ๊ฒ€์‚ฌ)
		}else if(index < 0) {
			return "์Œ์ˆ˜๋Š” ์ž…๋ ฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค";
			
			// 3) ๋งŒ์•ฝ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ -> ์˜ˆ์™ธ์ฒ˜๋ฆฌ throws
		
		// 4) ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๊ฐ€ studentList ๋ฒ”์œ„ ๋‚ด ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์ธ๊ฐ€?	
		}else if(index >= studentList.size()) {
			return "๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„  ๊ฐ’์„ ์ž…๋ ฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.";
		
		}else {
			
			// ํ•™์ƒ ์ •๋ณด ์ œ๊ฑฐ
			System.out.print("์ •๋ง ์‚ญ์ œ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (Y/N) : ");
			char ch = sc.next().toUpperCase().charAt(0);
			// String.toUpperCase() : ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ
			
			if(ch == 'Y') {
				Student temp = studentList.remove(index);
				return temp.getName() + "์˜ ์ •๋ณด๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."; 
			}else {
				return "์ทจ์†Œ";
			}
		}
		
	}
	
	/**
	 * ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰(์ „์ฒด ์ผ์น˜)
	 */
	public void searchName() {
		
		// ๊ฒ€์ƒ‰ํ•  ์ด๋ฆ„ ์ž…๋ ฅ : 
		// ์š”์†Œ ์ •๋ณด ์ถœ๋ ฅ
		// ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

		System.out.println("==========ํ•™์ƒ ๊ฒ€์ƒ‰(์ด๋ฆ„ ์ผ์น˜)==========");
		
		System.out.print("๊ฒ€์ƒ‰ํ•  ์ด๋ฆ„ ์ž…๋ ฅ : ");
		String input = sc.next();
		
		boolean flag = true;
		
		// ํ–ฅ์ƒ๋œ for๋ฌธ
		for( Student std : studentList ) {
			
			if( input.equals(std.getName())) { // ์ด๋ฆ„์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ
				System.out.println(std);
				flag = false;
			}
		}
		
		if(flag) {
			System.out.println("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
		}
		
	}

	/**
	 * ์ด๋ฆ„์— ํŠน์ • ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜๋Š” ํ•™์ƒ์„ ์ฐพ์•„์„œ ์กฐํšŒํ•˜๋Š” ๋ฉ”์„œ๋“œ
	 */
	public void searchName2() {
		
		System.out.println("==========ํ•™์ƒ ๊ฒ€์ƒ‰(๋ฌธ์ž์—ด ํฌํ•จ)==========");
		
		System.out.print("์ด๋ฆ„์— ํฌํ•จ๋˜๋Š” ๋ฌธ์ž์—ด ์ž…๋ ฅ : ");
		String input = sc.next();

		boolean flag = true;
		
		// ํ–ฅ์ƒ๋œ for๋ฌธ
		for( Student std : studentList ) {
			
			// boolean String.contains(๋ฌธ์ž์—ด) : String์— ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด์žˆ์œผ๋ฉด true ์•„๋‹ˆ๋ฉด false
			if( std.getName().contains(input)) { 
				System.out.println(std);
				flag = false;
			}
		}
		
		if(flag) {
			System.out.println("๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
		}
		
	}
	
}

โœ… StudentRaun ํด๋ž˜์Šค

package edu.kh.collection.run;

import edu.kh.collection.model.service.StudentService;

public class StudentRun {

	public static void main(String[] args) {
		
		StudentService service = new StudentService();
		
		service.displayMenu();

	}

}

0๊ฐœ์˜ ๋Œ“๊ธ€