๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
FOR SW ENGINEER๐Ÿ‘ฉ‍๐Ÿ’ป/JAVA

๊ตญ๋น„JAVA์ˆ˜์—… DAY-9

by ์”จ๋‹ˆ 2021. 8. 24.
728x90

์˜ค๋Š˜์€ ๊ตญ๋น„์ˆ˜์—… 9์ผ์ฐจ!! 

์ ์  ๋” ์–ด๋ ค์›Œ ์ง€๊ณ  ์žˆ๋‹ค..

8์ผ์ฐจ๊นŒ์ง€๋Š” Java์˜ ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ๋“ค์„ ๋ฐฐ์šฐ๋Š๋ผ ์ˆ˜์›”ํ–ˆ์ง€๋งŒ,, ์—ญ์‹œ๋‚˜ ๋ฐฐ์—ด์ด๋ž‘ ํ•จ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋Š” ์ˆœ๊ฐ„ ์–ด๋ ค์›Œ์ง„๋‹น ใ…œใ…œ

ํ•™๊ต ๋‹ค๋‹๋•Œ ์—ด์‹ฌํžˆ ์ข€ ํ•ด๋†“์„๊ฑธ,,, ์ด๋ฒˆ์—” ๋„์ค‘์— ์–ด๋ ต๋‹ค๊ณ  ํฌ๊ธฐํ•˜์ง€ ๋ง์ž!!

์ด๋ฒˆ 9์ผ์ฐจ๋Š” ๋ฐฐ์—ด ์กฐ๊ธˆ ์‹ฌํ™”๊ณผ์ •๊ณผ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋Š”๋ฐ ์ด๋ฒˆ ๋กœ๊ทธ์—์„œ๋Š” ๋ฐฐ์—ด์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค!!

๊ทธ๋Ÿผ ๋ฐฐ์—ด(INSERT, UPDATE, DELETE)์˜ˆ์ œ ์š”์  ์‹œ์ž‘~~๐Ÿ™Œ

 

 

 

<๋ฐฐ์—ด์˜ INSERT(์‚ฝ์ž…) ์˜ˆ์ œ>

package Day09;
import java.util.Arrays;

public class ArrayInsert {
	public static void main(String[] args) {
    	
        Scanner scan = new Scanner(System.in);
        String[] arr = new String[100]; //ํฌ๊ธฐ 100์ธ ๋ฐฐ์—ด ์ƒ์„ฑ
        int count = 0; //์Œ์‹๊ฐœ์ˆ˜ ์ €์žฅ๋ณ€์ˆ˜
        
        System.out.println("๋จน๊ณ  ์‹ถ์€ ์Œ์‹์ด ๋ญ์—์š”?");
		System.out.println("์ž…๋ ฅ๋งŒ ํ•œ๋ฒˆ ํ•ด๋ณด์„ธ์š”.");
        
        while(true) {
        	System.out.print(">");
            String menu = scan.nextLine(); //๋ฉ”๋‰ด ์ž…๋ ฅ๋ฐ›์Œ
            
            if(menu.equals("๊ทธ๋งŒ")) { //๋ฌธ์ž์—ด์€ '=='๋™๋“ฑ๊ธฐํ˜ธ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ ex) menu == "๊ทธ๋งŒ" xxx
            	System.out.println("์ž…๋ ฅ์ข…๋ฃŒ!");
                break; 
            }
            
            arr[count] = menu;
            count++; //์ž…๋ ฅ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค count 1์”ฉ ์ฆ๊ฐ€
         }
         /* System.out.println(Arrays.toString(arr));  
         * <-- ์œ„์—์„œ ์„ ์–ธํ•œ ๋ฐฐ์—ด arrํฌ๊ธฐ๊ฐ€ 100์ด๊ธฐ ๋•Œ๋ฌธ์— 
         * Arrays.toString์„ ์“ฐ๋ฉด ์ž…๋ ฅ๋ฐ›์ง€ ์•Š์€ ๊ณต๊ฐ„์€ Null๊ฐ’์œผ๋กœ ์ถœ๋ ฅ๋จ.
         */
         System.out.print("๋จน๊ณ ์‹ถ์€ ์Œ์‹:[");
		 for(int i = 0; i < count; i++) {
			 System.out.print(arr[i]);
			 if(i == count - 1) break;
			 System.out.print(",");
		 }
		 System.out.println("]");
	}
}

 

<๋ฐฐ์—ด์˜ UPDATE ์˜ˆ์ œ>

package Day09;
import java.util.Arrays;

public class ArrayUpdate{ //๋ฐฐ์—ด์˜ ๋ณ€๊ฒฝ
	public static void main(String[] args){ //์ž…๋ ฅ -> ์ •๋ณด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํƒ์ƒ‰ -> ๋ณ€๊ฒฝ
    	
        Scanner scan = new Scanner(System.in);
        String[] arr = {"๋‚จ์ค€", "์„์ง„", "์œค๊ธฐ", "ํ˜ธ์„", "์ง€๋ฏผ", "ํƒœํ˜•", "์ •๊ตญ"};
        
        System.out.print("์ˆ˜์ •ํ•  ๋ณ„๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š”>");
        String name = scan.next(); //์ˆ˜์ •ํ•  ์ด๋ฆ„ ์ž…๋ ฅํ•˜๊ธฐ(๋ฐฐ์—ด์— ์žˆ๋Š” ์ด๋ฆ„)
        
        boolean flag = false; // If๋ฌธ์ด ์‹คํ–‰๋œ๊ฑธ ํ™•์ธํ•˜๋Š” ๊ฑธ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธ
        for(int i = 0; i < arr.length; i++){
        	if(name.equals(arr[i])) { //๋ฐฐ์—ด์— ์ž…๋ ฅํ•œ ์ด๋ฆ„ ์žˆ์œผ๋ฉด ์‹คํ–‰
            	System.out.print("๋ณ€๊ฒฝํ•  ์ด๋ฆ„์„ ์ž‘์„ฑํ•˜์„ธ์š”>");
                String s = scan.next(); //๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ์ด๋ฆ„ ์ƒˆ๋กœ ๋ฐ›์Œ
                arr[i] = s;
                flag = true; //if๋ฌธ ์‹คํ–‰ํ–ˆ๋‹ค๋Š” ์ฆ๊ฑฐ
            }
        }
        
        if(flag) { //flag๊ฐ€ true์ผ๋•Œ ์‹คํ–‰
        	System.out.println("์ด๋ฆ„์ด ์ •์ƒ์ ์œผ๋กœ ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");
            System.out.println(Arrays.toString(arr)); //๋ฐฐ์—ด ์ถœ๋ ฅ
         }
         else {
         	System.out.println("์ฐพ๋Š” ์ด๋ฆ„์ด ์—†์Šต๋‹ˆ๋‹ค!");
         }
     }
}

-์—ฌ๊ธฐ์„œ if(flag)๊ตฌ๋ฌธ์ด ํ—ท๊ฐˆ๋ ค์„œ ๊ฐ•์‚ฌ๋‹˜๊ป˜ ๋‹ค์‹œ ์งˆ๋ฌธํ•ด์•ผ๊ฒ ๋‹ค!(๋ฌด์กฐ๊ฑด if(flag)๋Š” flag๊ฐ€ true์ด๋ฉด ์‹คํ–‰๋˜๋Š”๊ฑด์ง€!)

 

<๋ฐฐ์—ด์˜ DELETE(์‚ญ์ œ) ์˜ˆ์ œ>

package Day09;
import java.util.Scanner;

public class ArrayDelete{
	public static void main(String[] args){
   
//      4๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ์‚ญ์ œ ํ‘œํ˜„
//		int[] arr = {1,2,3,4,5,6,7,8,9,10};
//		for(int i = 4; i < arr.length-1; i++) {
//			arr[i] = arr[i+1];
//		}
//		System.out.println(Arrays.toString(arr));
		
        
        //์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๋ณ€์ˆ˜ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด ๋‚ด ์ •๋ณด ์‚ญ์ œ
        //๋ณ„๋ช…์ž…๋ ฅ -> ํƒ์ƒ‰ -> ๋‹น๊ฒจ์˜ค๊ธฐ
        Scanner scan = new Scanner(System.in);
        String[] arr = {"๋‚จ์ค€", "์„์ง„", "์œค๊ธฐ", "ํ˜ธ์„", "์ง€๋ฏผ", "ํƒœํ˜•", "์ •๊ตญ"};
		String name = scan.next();
        
        int count = arr.length; //์‚ฌ๋žŒ์ˆ˜(๋ณ€์ˆ˜ํ™”)
        boolean flag = false; // ์‚ญ์ œ์—ฌ๋ถ€ ํ™•์ธ ์œ„ํ•œ ๋ณ€์ˆ˜
        
        for(int i = 0; i < count; i++) {
        	if(name.equals(arr[i])) { //์‚ญ์ œํ•  ์‚ฌ๋žŒ์„ ์ฐพ์€ ๊ฒฝ์šฐ
            	System.out.println(name + "์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.");
                
                //๋’ค์— ์ธ๋ฑ์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ๋‹น๊ฒจ์˜ค๊ธฐ(์‚ญ์ œ ๋œ ๋ถ€๋ถ„์„ ์ฑ„์›Œ์ฃผ๋ฉด์„œ ํ•˜๋‚˜์”ฉ ์•ž์œผ๋กœ ๋‹น๊ฒจ์คŒ)
                for(int j = il j < count -1; i++){
                	arr[j] = arr[j+1];
                }
                
                //์‚ญ์ œ์˜ ์˜๋ฏธ
                count --;
                flag = true; //์‚ญ์ œ๋˜๋ฉด true๋กœ ๋ฐ”๋€Œ์–ด์„œ ์‚ญ์ œ๋œ๊ฑธ๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Œ
            }
        }
        
        //๊ฒฐ๊ณผ
        if(flag) { //์‚ญ์ œ ๋œ๊ฒŒ ํ™•์ธ ๋จ.
        	for(int i = 0; i < count; i++) {
            	System.out.println("======์‚ญ์ œ ํ›„ ์ •๋ณด======");
				System.out.print(arr[i] + " ");
			}
            System.out.println("\n=======================");
		}
		else {
			System.out.println("์‚ญ์ œ ํ•  ๋ณ„๋ช…์ด ์—†์Šต๋‹ˆ๋‹ค.");
		}
    }
}

 

<switch case๋ฅผ ์ด์šฉํ•œ ๋ฐฐ์—ด ๋ฌธ์ œ - ๋ฉ”๋‰ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๊ณ ๊ฐ ์ •๋ณด ์ˆ˜์ •ํ•˜๊ธฐ>

์•ž ๋ถ€๋ถ„

Case1) ์ˆซ์ž 1์„ ์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ,  ๋ฐฐ์—ด์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.

-์ด๋ฆ„, ์„ฑ๋ณ„, ๋‚˜์ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฐ’ ๋„ฃ์–ด์ฃผ๊ธฐ

Case2) ์ˆซ์ž 2๋ฅผ์ž…๋ ฅ๋ฐ›์•˜์„ ๋•Œ, ํ˜„์žฌ์œ„์น˜(index)์˜ ์ „ ๋ฐฐ์—ด๋‚ด์— ์žˆ๋Š” ๊ฐ’ ๋ณด์—ฌ์ฃผ๊ธฐ

- ๋ฐฐ์—ด์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0์ดํ•˜๋ฉด ์ด์ „ ์ •๋ณด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—,

- 1์ด์ƒ์ผ๋•Œ๋งŒ index๊ฐ’ 1์„ ์ค„์—ฌ ํ˜„์žฌ์œ„์น˜ ์ „ ๋ฐฐ์—ด ๊ฐ’ ๋ณด์—ฌ์ฃผ๋„๋ก ํ•œ๋‹ค.

case3) ์ˆซ์ž 3์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ, ํ˜„์žฌ์œ„์น˜์˜ ๋‹ค์Œ ๋ฐฐ์—ด ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

- case2์™€ ๋น„์Šทํ•œ๋ฐ,

- count๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ์„ธ๋ฉด์„œ ์˜ฌ๋ผ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด 0๊ฐ’์— ์ •๋ณด๊ฐ€ ํ•˜๋‚˜ ๋“ค์–ด๊ฐ€๋ฉด count๋Š” 1, ๋ฐฐ์—ด 1๊ฐ’์— ์ •๋ณด ํ•˜๋‚˜ ๋“ค์–ด๊ฐ€๋ฉด count๋Š” 2๋กœ ๋ฐฐ์—ด์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” index๋ณด๋‹ค count๊ฐ€ 1์ด ๋” ํฌ๋‹ค.

- ๋”ฐ๋ผ์„œ count -1๋ณด๋‹ค index ๊ฐ’์ด ํด๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰์ด๋‚˜, ๊ทธ ์ด์ƒ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์ •๋ณด๋Š” ์—†๋‹ค.

- ๊ทธ๋ž˜์„œ ๊ทธ ์™ธ์˜  " -1 <= index <= count-1" ์ผ ๋•Œ๋งŒ ๋‹ค์Œ ๋ฐฐ์—ด์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

case4) ์ˆซ์ž 4์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ, ํ˜„์žฌ์œ„์น˜์˜ ๋ฐฐ์—ด ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

- case3์—์„œ ๋งํ–ˆ๋‹ค ์‹œํ”ผ index = count -1 ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ์œ„์น˜๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด

- index์˜ ์กฐ๊ฑด์€ 0 <= index <= count -1(๋ฐฐ์—ด๋‚ด ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๊ณณ๊นŒ์ง€๋งŒ ์ถœ๋ ฅ)

- index์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค๋ฉด ํ˜„์žฌ์˜ ๋ฐฐ์—ด์ •๋ณด ์ถœ๋ ฅ.

case5) ์ˆซ์ž 5์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ, ํ˜„์žฌ์œ„์น˜์˜ ๋ฐฐ์—ด ๋‚ด ์ •๋ณด ์›ํ•˜๋Š” ์ •๋ณด๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

- ํ˜„์žฌ ์ •๋ณด๊ฐ€ ์ž…๋ ฅ๋˜์–ด์žˆ๋Š” ๋ฐฐ์—ด 0 <= index <= count-1 ๋‚ด์—์„œ ํ˜„์žฌ ์œ„์น˜์˜ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์„๋•Œ

- ์ˆซ์ž 5๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, ํ˜„์žฌ ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์— ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

case6) ์ˆซ์ž 6์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ, ํ˜„์žฌ์œ„์น˜์˜ ์ •๋ณด ์‚ญ์ œ

- ์กฐ๊ฑด์€ case 4~5์™€ ๊ฐ™์ด ๋ฐฐ์—ด ๋‚ด ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋Š” 0 <= index <= count -1๋‚ด์—์„œ

- ์ˆซ์ž 6์„ ์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ indexํ˜„์žฌ ์œ„์น˜์˜ ์ •๋ณด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

case7) ์ˆซ์ž 7์ž…๋ ฅ๋ฐ›์•˜์„๋•Œ, ๋ชจ๋“  ๋ฃจํ”„๋ฅผ ํƒˆ์ถœํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ.

++์˜ค๋Š˜ ์ฒ˜์Œ ๋ฐฐ์šด๊ฒƒ!! ++

System.exit(0) ==> ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋Š” ์‹คํ–‰๋ฌธ

default) case์™ธ์˜ ์ƒํ™ฉ.

728x90