Java programming

(22.10.19)Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์˜ˆ์™ธ์ฒ˜๋ฆฌ Exception ์ปฌ๋ ‰์…˜ Collection

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 10. 19.

**๋ฌธ์ž์—ด ๋ฐฐ์—ด ์›์†Œ 10๊ฐœ๋ฅผ ํšŒ์›์˜ ์ด๋ฆ„์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ํ‚ค๋ณด๋“œ์—์„œ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์› ์ด๋ฆ„์„ ํ‘œ์‹œํ•œ๋‹ค. ๋งŒ์•ฝ ์ด์šฉ์ž๊ฐ€ 0~9์ด์™ธ์˜ ์ˆ˜์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ๋น„์ •์ƒ ์ข…๋ฃŒ๋˜์ง€ ์•Š๋„๋ก ๊ฐœ์„ ํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Scanner;
public class ExceptionMain2 
{
    public static void main(String[] args) 
    {
        String[] str = new String[10];
        str[0]="๋ฐ•์ฃผํ˜„";
        str[1]="๊น€์‚ผ์‚ผ";
        str[2]="์˜ค์ผ์˜";
        str[3]="์œค์„์—ด";
        str[4]="๋ฌธ์žฌ์ธ";
        str[5]="๋ฐ•๊ทผํ˜œ";
        str[6]="์ด๋ช…๋ฐ•";
        str[7]="๋…ธ๋ฌดํ˜„";
        str[8]="๊น€๋Œ€์ค‘";
        str[9]="๊น€์˜์‚ผ";
        Scanner kbd = new Scanner(System.in);
        while(true) {
            System.out.print("0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.\n");
            int num = kbd.nextInt();
            try {
                System.out.println("๊ฒฐ๊ณผ:"+ str[num]);
                break;
            }
            catch(ArrayIndexOutOfBoundsException ie){
                String msg = ie.getMessage();
                System.err.println("์—๋Ÿฌ์›์ธ:"+msg);
                System.err.println("0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
            }
        }
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    }
}
cs

์ฝ˜์†” ๊ฐ’:

0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
11
์—๋Ÿฌ์›์ธ:Index 11 out of bounds for length 10
0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.
7
๊ฒฐ๊ณผ:๋…ธ๋ฌดํ˜„
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ


๊ตณ์ด ์ด์ฐจ์› ๋ฐฐ์—ด์„ ์จ์„œ ํ•œ๋ฒˆ ํ•ด๋ณด์•˜๋‹ค..................๋งค์šฐ ๋น„ํšจ์œจ์ ์ด์ง€๋งŒ ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
public class ExceptionMain 
{
    public static void main(String[] args) 
    {
        String[][] str = new String[10][2];
        str[0][1]="๋ฐ•์ฃผํ˜„";
        str[1][1]="๊น€์‚ผ์‚ผ";
        str[2][1]="์˜ค์ผ์˜";
        str[3][1]="์œค์„์—ด";
        str[4][1]="๋ฌธ์žฌ์ธ";
        str[5][1]="๋ฐ•๊ทผํ˜œ";
        str[6][1]="์ด๋ช…๋ฐ•";
        str[7][1]="๋…ธ๋ฌดํ˜„";
        str[8][1]="๊น€๋Œ€์ค‘";
        str[9][1]="๊น€์˜์‚ผ";
        int i=0;
        for(;i<str.length;i++) {
            str[i][0]="i";    
        }
        Scanner kbd = new Scanner(System.in);
        while(true) {
            System.out.print("0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.\n");
            int num = kbd.nextInt();
            try {
                System.out.println("๊ฒฐ๊ณผ:"+ str[num][1]);
                
                break;
            }
            catch(ArrayIndexOutOfBoundsException ie){
                System.err.println("0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
            }
        }
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    }
}
cs

์ฝ˜์†” ๊ฐ’:

0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
11
0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.
8
๊ฒฐ๊ณผ:๊น€๋Œ€์ค‘
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ


catch(Exception ex)์€ ๋ชจ๋“  ์˜ˆ์™ธ๋ฅผ ๋‹ค ๊ฐ€๋ฅดํ‚จ๋‹ค.

return;๊ฒฝ์šฐ finally๊นŒ์ง„ ๋Œ์•„๊ฐ„๋‹ค.

ํ•˜์ง€๋งŒ System.exit(0); ์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ฌธ๊ตฌ๊ฐ€ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊นŒ์ง€๋งŒ ์‹คํ–‰๋˜๊ณ  JVM์ด ๊ฐ•์ œ ์ข…๋ฃŒ๋ผ์„œ finally ๋ธ”๋Ÿญ์€ ๋Œ์•„๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

finally ๋ธ”๋Ÿญ์—๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚˜๋“  ์•ˆ๋‚˜๋“  ์ผ์–ด๋‚˜์•ผํ•  ์ผ์— ์จ์•ผํ•˜๋Š”๋ฐ ๊ทธ ์˜ˆ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ๋Š๊ธฐ๊ฐ€ ์žˆ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionMain2 
{
    public static void main(String[] args) 
    {
        String[] str = new String[10];
        str[0]="๋ฐ•์ฃผํ˜„";
        str[1]="๊น€์‚ผ์‚ผ";
        str[2]="์˜ค์ผ์˜";
        str[3]="์œค์„์—ด";
        str[4]="๋ฌธ์žฌ์ธ";
        str[5]="๋ฐ•๊ทผํ˜œ";
        str[6]="์ด๋ช…๋ฐ•";
        str[7]="๋…ธ๋ฌดํ˜„";
        str[8]="๊น€๋Œ€์ค‘";
        str[9]="๊น€์˜์‚ผ";
        
        while(true) {
            Scanner kbd = new Scanner(System.in);
            System.out.print("0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.\n");
            try {
                int num = kbd.nextInt();
                System.out.println("๊ฒฐ๊ณผ:"+ str[num]);
                break;
            }
            catch(Exception ex) {
                if(ex instanceof ArrayIndexOutOfBoundsException) {
                    String msg = ex.getMessage();
                    System.err.println("์—๋Ÿฌ์›์ธ:"+msg);
                    System.err.println("0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
                }
                else if(ex instanceof InputMismatchException) {
                    System.err.println("์ˆซ์ž๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
                }
                System.err.println("๊ธฐํƒ€ ์—๋Ÿฌ.");
            }
            finally {
                //์—๋Ÿฌ๊ฐ€ ๋‚˜๋“ , ์•ˆ๋‚˜๋“  ์‹คํ–‰ํ•  ์‹คํ–‰๋ฌธ
            }
        }
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    }
}
cs

์ฝ˜์†” ๊ฐ’:

0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
12
์—๋Ÿฌ์›์ธ:Index 12 out of bounds for length 10
0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.
0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
a
์ˆซ์ž๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.
0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
2
๊ฒฐ๊ณผ:์˜ค์ผ์˜
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ


Scanner kbd = new Scanner(System.in); ๋ฌธ์žฅ์ด while๋ฌธ ๋ฐ–์— ์žˆ์œผ๋ฉด ์ฝ˜์†” ๊ฐ’์ด ๋ฌดํ•œ์œผ๋กœ ๋ณต์‚ฌ๊ฐ€๋œ๋‹ค. 

Scanner kbd = new Scanner(System.in);์ด while๋ฌธ ์•ˆ์— ์žˆ์–ด์•ผ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค. 

๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด, ์ฆ‰ Scanner kbd = new Scanner(System.in);์„ while๋ฌธ ๋ฐ–์— ์“ด๋‹ค๋ฉด 

else if(ex instanceof InputMismatchException) {
	System.err.println("์ˆซ์ž๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
	kbd.nextLine();
}

else if๋ฌธ์—์„œ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ๋น„์›Œ์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— kbd.nextLine();์ด ๋“ค์–ด๊ฐ€์•ผํ•œ๋‹ค.

๋˜ finally๋ฌธ์„ ์ด์šฉํ•˜์—ฌ finally๋ฌธ์— kbd.nextLine();์— ๋„ฃ์–ด์ฃผ๋ฉด ํ•œ๋ฒˆ์”ฉ ๋๋‚ ๋•Œ ๋งˆ๋‹ค ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ๋น„์›Œ์ฃผ๊ธฐ๋•Œ๋ฌธ์— ๊น”๋”ํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„๋ฆด ๋ชฉ์ !

์˜ˆ์‹œ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class ExceptionMain2 
{
    public static void main(String[] args) 
    {
        String[] str = new String[10];
        str[0]="๋ฐ•์ฃผํ˜„";
        str[1]="๊น€์‚ผ์‚ผ";
        str[2]="์˜ค์ผ์˜";
        str[3]="์œค์„์—ด";
        str[4]="๋ฌธ์žฌ์ธ";
        str[5]="๋ฐ•๊ทผํ˜œ";
        str[6]="์ด๋ช…๋ฐ•";
        str[7]="๋…ธ๋ฌดํ˜„";
        str[8]="๊น€๋Œ€์ค‘";
        str[9]="๊น€์˜์‚ผ";
        Scanner kbd = new Scanner(System.in);
        while(true) {
            System.out.print("0์—์„œ9๊นŒ์ง€์ค‘ ํ™•์ธํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.\n");
            try {
                int num = kbd.nextInt();
                System.out.println("๊ฒฐ๊ณผ:"+ str[num]);
                break;
            }
            catch(Exception ex) {
                if(ex instanceof ArrayIndexOutOfBoundsException) {
                    String msg = ex.getMessage();
                    System.err.println("์—๋Ÿฌ์›์ธ:"+msg);
                    System.err.println("0~9๊นŒ์ง€๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
                }
                else if(ex instanceof InputMismatchException) {
                    System.err.println("์ˆซ์ž๋งŒ ์ž…๋ ฅํ•˜์„ธ์š”.");
                }
                else System.err.println("๊ธฐํƒ€ ์—๋Ÿฌ.");
            }
            finally {
                kbd.nextLine();
            }
        }
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    }
}
cs

 


**๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

ํ‚ค๋ณด๋“œ์—์„œ ์•„์ด๋””์™€ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์•„์ด๋””๊ฐ€ "smith"์ด๊ณ  ์•”ํ˜ธ๊ฐ€ "1234"์ธ ๊ฒฝ์šฐ์—๋Š” '๋กœ๊ทธ์ธ ์„ฑ๊ณต'๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋„๋กํ•˜๊ณ  ๋กœ๊ทธ์ธ ์‹คํŒผ์œผ๋ฉด ์‹คํŒจ์‹œ ๋กœ๊ทธ์ธ ๋ฐ˜๋ณต

 

 

RuntimeException ์‹คํ–‰์ค‘ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ - ์ปดํŒŒ์ผ๋Ÿฌ์™€ ๋ฌด๊ด€ํ•˜๋‹ค ๊ทธ๋ž˜์„œ try catch๋กœ ๊ฐ์‹ธ์ฃผ์ง€ ์•Š์•„๋„๋œ๋‹ค.

 

Main ํด๋ž˜์Šค ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
 
public class ExceptionMain 
{
    public static void main(String[] args) 
    {
        boolean success = false;
            
        try {
            success = login();
        } catch (LoginFailException lfe) {
            System.err.println(lfe.getMessage());
        }
        if(success) {
            System.out.println("๋กœ๊ทธ์ธ ์„ฑ๊ณต");    
        }
        System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
    }
    public static boolean login() throws LoginFailException{
        Scanner kbd = new Scanner(System.in);
        boolean success = false;
        for(int i=0;i<3;i++) {
            System.out.print("์•„์ด๋””์™€ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค.\n");
            String input = kbd.nextLine();
            String[] idpw = input.split(" ");
            String id = idpw[0];
            String pw = idpw[1];
            
            if(id.equals("smith"&& pw.equals("1234")) {
                success = true;
                break;
            }
        }
        if(!success) {
            throw new LoginFailException("๋กœ๊ทธ์ธ ์‹คํŒจ");
        }
        return success;
    }
}
        
cs

 

์ปค์Šคํ…€exception ํด๋ž˜์Šค

LoginFailExceptionํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์ธ Exception๋ฅผ ์ƒ์†ํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
public class LoginFailException extends Exception 
{
 
    public LoginFailException() {}
 
    public LoginFailException(String message) {
        super(message);
    }
}    
cs

 

 

์ฝ˜์†” ๊ฐ’:

์•„์ด๋””์™€ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค.
dfdf 1321
์•„์ด๋””์™€ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค.
adfd 1231
์•„์ด๋””์™€ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค.
adfd 1235
๋กœ๊ทธ์ธ ์‹คํŒจ
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

 

lfe.getMessage()์ด ๋กœ๊ทธ์ธ ์‹คํŒจ๊ฐ€ ๋‚˜์˜ค๋Š” ์ด์œ ๋Š”

throw new LoginFailException("๋กœ๊ทธ์ธ ์‹คํŒจ");

์—ฌ๊ธฐ์„œ ๋กœ๊ทธ์ธ ์‹คํŒจ๋ฅผ ๋˜์ ธ์คฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


์ปฌ๋ ‰์…˜

Collections :  ๊ฐ์ฒด๋ฅผ ๋‹ค์ˆ˜๊ฐœ ์ €์žฅ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ

์ž์ฃผ ์“ฐ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ : List, Set, Map

์ž๋ฃŒ๊ตฌ์กฐ ์„ค๋ช…
List ์ˆœ์„œ๊ฐ€ ์žˆ๊ณ , ์ข…๋ณต์„ ํ—ˆ์šฉํ•œ๋‹ค. (๊ฐ’๋งŒ ์ €์žฅ)
Set ์ˆœ์„œ๊ฐ€ ์—†๊ณ , ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.(๊ฐ’๋งŒ ์ €์žฅ)
Map key, value (๊ฐ’๊ณผ ๊ผฌ๋ฆฌํ‘œ๊นŒ์ง€ ์ €์žฅ)

์ปดํ“จํ„ฐ ๊ณตํ•™์—์„  map์€ ์—ฐ๊ฒฐํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. ํ‚ค์™€ ๋ฐธ๋ฅ˜๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค. ํ‚ค๋ฅผ ์•Œ์•„์•ผ ๋‚˜์ค‘์— ๋ฐธ๋ฅ˜๊ฐ’์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๋น…๋ฐ์ดํ„ฐ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‚ค๋ฅผ ํŒจ์‹ฑ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋Œ๋ ค ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ฐ”๊พผ๋’ค ๋‚˜์˜จ ์ˆซ์ž๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋กœ ์ €์žฅํ•œ๋‹ค. ์ž…์ถœ๋ ฅ์ด ๊ฐ€์žฅ ๋น ๋ฅด๋‹ค.


List ์˜ˆ์‹œ

Vector(์“ฐ๋ ˆ๋“œ safe,๊ฐ€์žฅ ๋Š๋ฆฌ๋‹ค), Arraylist(ํ™”๋ฉด์— ํ‘œ๊ธฐํ•  ๋•Œ ์šฉ์ด), LinkedList(์ˆ˜์ • ์‚ญ์ œ์— ์šฉ์ด)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
public class CollectionTest 
{
    public static void main(String[] args) 
    {
        ArrayList<String> namelist = new ArrayList<>();
        namelist.add("๊ฐ•ํ˜ธ๋™");
        namelist.add("์œ ์žฌ์„");
        namelist.add("๋ฐ•๊ทผ์ง€");
        namelist.add("์‹ฌ๋™์—ฝ");
        namelist.add("ํ•ด๋ฆฐ");
        namelist.add("๊ฐ•ํ˜ธ๋™");
        
        System.out.println("์›์†Œ ์ˆ˜:"+namelist.size());
        for(int i=0;i<namelist.size();i++) {
            String name= namelist.get(i);
            System.out.println(name);
        }
    }
}
cs

์ฝ˜์†” ๊ฐ’:

์›์†Œ ์ˆ˜:6
๊ฐ•ํ˜ธ๋™
์œ ์žฌ์„
๋ฐ•๊ทผ์ง€
์‹ฌ๋™์—ฝ
ํ•ด๋ฆฐ
๊ฐ•ํ˜ธ๋™

 

์ˆœ์„œ๋„ ์ง€์ผœ์ง€๊ณ  ์ค‘๋ณต๋„ ํ—ˆ์šฉํ•˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.


ArrayList์— ์ €์žฅ๋œ ๊ธฐ๋Šฅ ์ค‘ CRUD์— ํ•ด๋‹นํ•˜๋Š” ๊ธฐ๋Šฅ

Create -> .add

Read - >.get

Update-> .set

Delete- >.remove

 

ArrayList์€ ์‚ญ์ œํ•˜๋ฉด ์›์†Œ ํ•˜๋‚˜์”ฉ ์•ž๋‹น๊ฒจ์ง€๊ธฐ๋•Œ๋ฌธ์— ๋งŽ์€ ์–‘์˜ ๋ชฉ๋ก์„ ์ฒ˜๋ฆฌํ• ๋• ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿด๋• LinkedList๋ฅผ ์“ฐ๋Š”๊ฒŒ ๋” ์œ ์šฉํ•˜๋‹ค.

์˜ˆ์‹œ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.ArrayList;
 
public class CollectionTest 
{
    public static void main(String[] args) 
    {
        ArrayList<String> namelist = new ArrayList<>();
        namelist.add("๊ฐ•ํ˜ธ๋™");
        namelist.add("์œ ์žฌ์„");
        namelist.add("๋ฐ•๊ทผ์ง€");
        namelist.add("์‹ฌ๋™์—ฝ");
        namelist.add("ํ•ด๋ฆฐ");
        namelist.add("๊ฐ•ํ˜ธ๋™");
        
        System.out.println("์›์†Œ ์ˆ˜:"+namelist.size());
        for(int i=0;i<namelist.size();i++) {
            String name= namelist.get(i);
            System.out.println(name);
        }
        namelist.remove("๋ฐ•๊ทผ์ง€");
        namelist.set(3"์ด์ˆ˜๊ทผ");
        System.out.println("์›์†Œ ์ˆ˜:"+namelist.size());
        for(int i=0;i<namelist.size();i++) {
            String name= namelist.get(i);
            System.out.println(name);
        }
    }
}
cs

์ฝ˜์†” ๊ฐ’:

์›์†Œ ์ˆ˜:6
๊ฐ•ํ˜ธ๋™
์œ ์žฌ์„
๋ฐ•๊ทผ์ง€
์‹ฌ๋™์—ฝ
ํ•ด๋ฆฐ
๊ฐ•ํ˜ธ๋™
์›์†Œ ์ˆ˜:5
๊ฐ•ํ˜ธ๋™
์œ ์žฌ์„
์‹ฌ๋™์—ฝ
์ด์ˆ˜๊ทผ
๊ฐ•ํ˜ธ๋™

 

์›์†Œ์˜ ์‚ญ์ œ์™€ ์ˆ˜์ •์ด ๋œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


Arraylist ์ฐพ๊ธฐ ๊ธฐ๋Šฅ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
public class CollectionTest 
{
    public static void main(String[] args) 
    {
        ArrayList<String> namelist = new ArrayList<>();
        namelist.add("๊ฐ•ํ˜ธ๋™");
        namelist.add("์œ ์žฌ์„");
        namelist.add("๋ฐ•๊ทผ์ง€");
        namelist.add("์‹ฌ๋™์—ฝ");
        namelist.add("ํ•ด๋ฆฐ");
        namelist.add("๊ฐ•ํ˜ธ๋™");
        
        int idx = namelist.indexOf("ํ•ด๋ฆฐ");
        //์›์†Œ๋ฅผ ์ฐพ์œผ๋ฉด ์›์†Œ์— ํ•ด๋‹น๋˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ์•Œ๋ ค์ค€๋‹ค
        boolean found = namelist.contains("ํ•ด๋ฆฐ");
        //์›์†Œ๋ฅผ ์ฐพ์œผ๋ฉด ์žˆ์œผ๋ฉด true๋ฅผ ์—†์œผ๋ฉด false๋ฅผ ์•Œ๋ ค์ค€๋‹ค.
        System.out.printf("%b index=%d \n",found,idx);
    }
}
cs

์ฝ˜์†” ๊ฐ’:

true index=4 

 

5๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๋‹ค๊ณ  ์•Œ๋ ค์ค€๋‹ค.


**ํ‚ค๋ณด๋“œ์—์„œ 3๋ช…์˜ ํšŒ์› ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์—ฌ Arraylist ์—  ์ €์žฅํ•œ๋‹ค.

๊ทธ ๋ฆฌ์ŠคํŠธ์— ํŠน์ • ์ด๋ฆ„์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์กด์žฌํ•˜๋ฉด ํ™”๋ฉด์— ํ‘œ์‹œ ํ›„ ์‚ญ์ œํ•œ๋‹ค.

์‚ญ์ œ ๋œ ํ›„์— ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด ๋‚ด์šฉ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•ด๋ณด๋ผ

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.ArrayList;
public class CollectionTest 
{
    public static void main(String[] args) 
    {
        ArrayList<String> namelist = new ArrayList<>();
        Scanner kbd = new Scanner(System.in);
        for(int i=0;i<3;i++) {
            System.out.print("ํšŒ์›์ด๋ฆ„:\n");
            String input = kbd.nextLine();
            namelist.add(input);    
        }
        if(namelist.contains("๋ฏผ์ง€")) {
            if(namelist.remove("๋ฏผ์ง€")) {
                System.out.print("์‚ญ์ œ์„ฑ๊ณต\n");
            }
        }
        else {
            System.err.print("๊ฒ€์ƒ‰ ์‹คํŒจ\n");
        }
        for(int i=0;i<namelist.size();i++) {
            System.out.print(namelist.get(i));
        }
    }
}
cs

์ฝ˜์†” ๊ฐ’:

 

๋งŒ์•ฝ "๋ฏผ์ง€"๋ฅผ ๋„ฃ์ง€ ์•Š๋Š”๋‹ค๋ฉด 

 

์ฝ˜์†” ๊ฐ’:


Car ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด model, price, producer ์†์„ฑ์„ ๋„ฃ์–ด 5๋Œ€์˜ ์ •๋ณด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๊ณ  ์ •๋ณด๊ฐ€ ๋งž์œผ๋ฉด ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•˜๋„๋กํ•œ๋‹ค.

 

Main ํด๋ž˜์Šค ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.ArrayList;
public class CollectionTest 
{
    public static void main(String[] args) 
    {
        ArrayList<Car> carlist = new ArrayList<>();
        carlist.add(new Car("์†Œ๋‚˜ํƒ€","3000","08.12","ํ˜„๋Œ€"));
        carlist.add(new Car("k3","2000","07.26","๊ธฐ์•„"));
        carlist.add(new Car("๊ธฐ๋ธ”๋ฆฌ","13000","10.12","๋งˆ์„ธ๋ผํ‹ฐ"));
        carlist.add(new Car("911","20000","05.23","ํฌ๋ฅด์‰"));
        carlist.add(new Car("์•„๋ฒคํƒ€๋„๋ฅด","70000","04.17","๋žŒ๋ณด๋ฅด๊ธฐ๋‹ˆ"));
        
        String key = "๊ธฐ๋ธ”๋ฆฌ";
        for(int i =0;i<carlist.size();i++) {
            if(key.equals(carlist.get(i).getModel())) {
                carlist.get(i).printcar();
                break;
            }
        }
    }
}
cs

Car ํด๋ž˜์Šค ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.ArrayList;
public class Car 
{
    private String model;
    private String price;
    private String date;
    private String producer;
    
    public Car() {}
    public Car(String model, String price, String date, String producer) {
        this.setModel(model);
        this.setPrice(price);
        this.setDate(date);
        this.setProducer(producer);
    }
    public void printcar() {
        System.out.printf("%s\t%s\t%s\t%s\n", model, price, date, producer);
    }
    
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getProducer() {
        return producer;
    }
    public void setProducer(String producer) {
        this.producer = producer;
    }
}
cs

์ฝ˜์†” ๊ฐ’:

๊ธฐ๋ธ”๋ฆฌ 13000 10.12 ๋งˆ์„ธ๋ผํ‹ฐ

 

๋Œ“๊ธ€