**๋ฌธ์์ด ๋ฐฐ์ด ์์ 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 ๋ง์ธ๋ผํฐ
๋๊ธ