ArrayList μ€μ΅
**ν€λ³΄λλ‘λΆν° μ± μ μ μ(Author) μ 보λ₯Ό μ λ ₯λ°μμ Author κ°μ²΄ μμ±(μμ±:λ²νΈ μ΄λ¦ μ νλ²νΈ)
3μΈμ μ μ₯ μ 보λ₯Ό Arraylistμ μ μ₯νμ¬ printAuthor() λ©μλ νΈμΆνμ¬ printAuthor() λ©μλλ 리μ€νΈμ ν¬ν¨λ μ μ λͺ©λ‘μ νλ©΄μ νμνλ€
searchAuthor() λ©μλλ₯Ό νΈμΆνμ¬ λ¦¬μ€νΈμμ λ²νΈλ‘ κ²μνμ¬ μ 보λ₯Ό μ°Ύλ κΈ°λ₯μ μμ±νλ€.
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.ArrayList;
import java.util.Scanner;
import com.ezen.javaoop.Author;
public class CollectionTest
{
public static void main(String[] args)
{
ArrayList<Author> Authorlist = new ArrayList<>();
Scanner kbd = new Scanner(System.in);
for(int i=0;i<3;i++) {
System.out.print("λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.");
int num = kbd.nextInt();
String input1 = kbd.next();
String input2 = kbd.next();
kbd.nextLine();
Authorlist.add(new Author(num, input1, input2));
}
printAuthor(Authorlist);
System.out.print("κ²μν λ²νΈλ₯Ό μ
λ ₯νμΈμ.");
int findnum = kbd.nextInt();
Author found = searchAuthor(Authorlist,findnum);
found.printinfor();
}
public static void printAuthor( ArrayList<Author> Authorlist) {
for(int i =0;i<Authorlist.size();i++) {
Authorlist.get(i).printinfor();
}
}
public static Author searchAuthor(ArrayList<Author> Authorlist, int a) {
Author author = null;
for(int i =0;i<Authorlist.size();i++) {
author = Authorlist.get(i);
if(a==(author.getNum())) {
return author;
}
}
return null;
}
}
|
cs |
Author ν΄λμ€ μ½λ
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
|
public class Author {
private int num;
private String name;
private String phone;
public Author() {}
public Author(int num, String name, String phone) {
this.setNum(num);
this.setName(name);
this.setPhone(phone);
}
public void printinfor() {
System.out.printf("%d\t%s\t%s\n", num, name, phone);
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
|
cs |
μ½μ κ°:
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.11 dean 010-5465-1646
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.12 pual 010-5645-4148
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.13 bin 010-1564-3625
11 dean 010-5465-1646
12 pual 010-5645-4148
13 bin 010-1564-3625
κ²μν λ²νΈλ₯Ό μ
λ ₯νμΈμ.12
12 pual 010-5645-4148
μ΄μ κ°μ κ²½μ°μ namelist.add("κ°νΈλ"); λ‘ ArrayListμ λ¬Έμλ₯Ό λ°λ‘ λ΄μμ get(i)νλ©΄ μ μ₯λ κ°(κ°νΈλ)μ΄ λ°λ‘ λμμ§λ§, μ€λκ°μ κ²½μ°λ κ°μ²΄λ₯Ό λ΄μκΈ° λλ¬Έμ μ¦ Authorlist.add(new Author(num, input1, input2));
Author κ°μ²΄λ₯Ό λ΄μκΈ°λλ¬Έμ Authorlist.get(i) λ₯Ό print νλ©΄ μ°Έμ‘° μ£Όμλ§ λμ¨λ€. λ°λΌμ .toStringμ μ€λ²λΌμ΄λ ν΄μ μ°λκ° μλλ©΄ μμ κ°μ λ°©μμ²λΌ Author ν΄λμ€ μ½λμ printν νμμ λ©μλλ₯Ό λ§λ€λ©΄ λλ€.
searchAuthor λ©μλλ₯Ό Listμ μλ κΈ°λ₯μ μ¨μ μ½λλ₯Ό μ§λ³Έλ€λ©΄
.contains κΈ°λ₯μ μ¨μΌνλ€.
λ¨Όμ Main ν΄λμ€μ searchAuthor λ©μλμ μλ£νμ Authorμμ voidλ‘ λ°κΎΈκ³
1
2
3
4
5
6
7
|
public static void searchAuthor(ArrayList<Author> Authorlist, int a) {
Author key = new Author();
key.setNum(a);
boolean cont = Authorlist.contains(key);
System.out.println("ν¬ν¨μ¬λΆ:"+ cont);
}
|
cs |
Main ν΄λμ€μ searchAuthor() νΈμΆν λλ μμμ΄ λ°κΏμ€μΌ κ·Έλ₯
searchAuthor(Authorlist,findnum);
μ΄λ κ² νΈμΆν΄μ£Όλ©΄ λλ€. searchAuthorμμ νλ¦°νΈ λ°©μμ μ ν΄μ€¬κΈ° λλ¬Έμ΄λ€.
Author ν΄λμ€ μ½λ μμ .equalsλ₯Ό μ€λ²λΌμ΄λν΄μ μ«μ κ°μ΄ κ°μΌλ©΄ trueκ° λμ€λλ‘ overrideν΄μ€λ€.
1
2
3
4
5
6
7
8
|
@Override
public boolean equals(Object obj) {
Author z = (Author)obj;
if(this.num==z.num) {
return true;
}
return false;
}
|
cs |
μ½μ κ°:
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.14 bob 2325-151
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.15 james 2165-5498
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.16 jun 118-4541
14 bob 2325-151
15 james 2165-5498
16 jun 118-4541
κ²μν λ²νΈλ₯Ό μ
λ ₯νμΈμ.15
ν¬ν¨μ¬λΆ:true
ifλ¬Έμ λ κ°λ¨νκ² νννλ©΄
return this.num==z.num ? true : false
μ°Έμ΄ λμμλ μ 보λ₯Ό μ»μΌλ €κ³ νλ©΄ Main ν΄λμ€μ searchAuthor λ©μλμ ifλ¬Έμ μ¨μ νννλ©΄ λλ€.
κ·Έλ λ€λ©΄ λ€μ Main ν΄λμ€μ searchAuthor λ©μλμ μλ£νμ AuthorμΌλ‘ λ°κΏμ£Όκ³
1
2
3
4
5
6
7
8
9
|
public static Author searchAuthor(ArrayList<Author> Authorlist, int a) {
Author key = new Author();
key.setNum(a);
if(Authorlist.contains(key)) {
return Authorlist.get(Authorlist.indexOf(key));
}
return null;
}
}
|
cs |
μμ λ©μΈ ν΄λμ€μ νΈμΆ λ°©μλ λ€μ μλ ννλ‘ λ°κΏμ€λ€.
System.out.print("κ²μν λ²νΈλ₯Ό μ
λ ₯νμΈμ.");
int findnum = kbd.nextInt();
Author found = searchAuthor(Authorlist,findnum);
found.printinfor();
μ½μ κ°:
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.1 2 3
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.4 5 6
λ²νΈ μ΄λ¦ μ νλ₯Ό μ
λ ₯νμΈμ.7 8 9
1 2 3
4 5 6
7 8 9
κ²μν λ²νΈλ₯Ό μ
λ ₯νμΈμ.7
7 8 9
**Listλ₯Ό μ΄μ©ν CRUD μ€μ΅
ProductλΌλ κ°μ²΄λ₯Ό λ§λ€μ΄ modelno, name, price, date 4κ°μ μμ±μ κ°κ² νλΌ.
ArrayListλ₯Ό μ¬μ©νμ¬ λ§λ€μ΄λΌ μμμμ λ©λ΄ νμκ° λμ€κ² νλΌ
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
import java.util.ArrayList;
import java.util.Scanner;
public class ExceptionMain2
{
static ArrayList<Product> product = new ArrayList<>();
static enum Menu{LIST, ADD, FIND, UPDATE, DELETE, EXIT};
public static void main(String[] args)
{
boolean go = true;
Menu menu = null; //MenuInputException
while(go) {
menu = menu();
if(menu==null) {
System.err.println("λ©λ΄μ
λ ₯μ€λ₯");
continue;
}
switch(menu) {
case LIST: listProduct(); break;
case ADD : addProduct(); break;
case FIND: findProduct(); break;
case UPDATE: updateProduct(); break;
case DELETE: deleteProduct(); break;
case EXIT : go =false; break;
default: System.err.println("λ©λ΄μ
λ ₯μ€λ₯");
}
}
System.out.println("νλ‘κ·Έλ¨ μ’
λ£");
}
public static Menu menu() {
System.out.println("λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)");
System.out.println("μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.");
Scanner kbd = new Scanner(System.in);
String input = kbd.nextLine().strip();
Menu menu = null;
switch(input) {
case "s" : menu = Menu.LIST; break;
case "a" : menu = Menu.ADD; break;
case "f" : menu = Menu.FIND; break;
case "u" : menu = Menu.UPDATE; break;
case "d" : menu = Menu.DELETE; break;
case "x" : menu = Menu.EXIT; break;
default : menu = null;
}
return menu;
}
public static void addProduct() {
Scanner kbd = new Scanner(System.in);
System.out.print("λ²νΈλ₯Ό μ
λ ₯νμΈμ.");
int num = kbd.nextInt();
kbd.nextLine();
System.out.print("μ΄λ¦μ μ
λ ₯νμΈμ.");
String input1 = kbd.nextLine().strip();
System.out.print("κ°κ²©μ μ
λ ₯νμΈμ.");
int price = kbd.nextInt();
kbd.nextLine();
System.out.print("λ μ§λ₯Ό μ
λ ₯νμΈμ.");
String input2 = kbd.nextLine().strip();
Product prod = new Product();
prod.setModelno(num);
prod.setName(input1);
prod.setPrice(price);
prod.setDate(input2);
product.add(prod);
}
public static void listProduct() {
for(int i=0;i<product.size();i++) {
product.get(i).printProduct();
}
}
public static void findProduct() {
Scanner kbd = new Scanner(System.in);
System.out.print("μ°Ύμ λ²νΈλ₯Ό μ
λ ₯νμΈμ.");
int num = kbd.nextInt();
Product key = new Product();
key.setModelno(num);
if(product.indexOf(key)<0){ //λ§μ½ μλ€λ©΄ -1μ΄ λμ¨λ€.
System.err.println("κ²μμ€ν¨");
}
else if(product.indexOf(key)>=0) {
product.get(product.indexOf(key)).printProduct();
}
}
public static void updateProduct() {
System.out.print("μμ ν λͺ¨λΈμ μ΄λ¦μ μ
λ ₯νμΈμ.");
Scanner kbd = new Scanner(System.in);
String name = kbd.nextLine();
for(int i=0;i<product.size();i++) {
if(name.equals(product.get(i).getName())) {
System.out.print("μμ ν λ²νΈ, λͺ¨λΈλͺ
, κ°κ²©, λ μ§λ₯Ό μ
λ ₯νμΈμ.");
int no = kbd.nextInt();
String input1 = kbd.next();
int price = kbd.nextInt();
String input2 = kbd.nextLine();
product.set(i, new Product (no,input1,price,input2 ));
}
}
}
public static void deleteProduct() {
System.out.print("μμ ν λͺ¨λΈμ λ²νΈλ₯Ό μ
λ ₯νμΈμ.");
Scanner kbd = new Scanner(System.in);
int num = kbd.nextInt();
for(int i=0;i<product.size();i++) {
if(num==product.get(i).getModelno()) {
product.remove(i);
}
}
}
}
|
cs |
Product ν΄λμ€ μ½λ
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
45
46
47
48
|
public class Product
{
private int modelno;
private String name;
private int price;
private String date;
public Product() {}
public Product(int modelno, String name, int price, String date) {
this.setModelno(modelno);
this.setName(name);
this.setPrice(price);
this.setDate(date);
}
public void printProduct() {
System.out.printf("%d\t%s\t%d\t%s\n", modelno, name, price, date );
}
@Override
public boolean equals(Object obj) {
Product other = (Product)obj;
return this.modelno==other.modelno ? true : false;
}
public int getModelno() {
return modelno;
}
public void setModelno(int modelno) {
this.modelno = modelno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
|
cs |
μ½μ κ°:
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
a
λ²νΈλ₯Ό μ
λ ₯νμΈμ.11
μ΄λ¦μ μ
λ ₯νμΈμ.jun
κ°κ²©μ μ
λ ₯νμΈμ.3000
λ μ§λ₯Ό μ
λ ₯νμΈμ.05.23
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
a
λ²νΈλ₯Ό μ
λ ₯νμΈμ.12
μ΄λ¦μ μ
λ ₯νμΈμ.bob
κ°κ²©μ μ
λ ₯νμΈμ.4000
λ μ§λ₯Ό μ
λ ₯νμΈμ.07.12
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
s
11 jun 3000 05.23
12 bob 4000 07.12
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
f
μ°Ύμ λ²νΈλ₯Ό μ
λ ₯νμΈμ.12
12 bob 4000 07.12
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
u
μμ ν λͺ¨λΈμ μ΄λ¦μ μ
λ ₯νμΈμ.bob
μμ ν λ²νΈ, λͺ¨λΈλͺ
, κ°κ²©, λ μ§λ₯Ό μ
λ ₯νμΈμ.13 paul 7000 06.17
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
s
11 jun 3000 05.23
13 paul 7000 06.17
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
d
μμ ν λͺ¨λΈμ λ²νΈλ₯Ό μ
λ ₯νμΈμ.13
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
s
11 jun 3000 05.23
λͺ©λ‘(s), μΆκ°(a), κ²μ(f), μμ (u), μμ (d), μ’
λ£(x)
μνλ λ©λ΄λ₯Ό λλ₯΄μΈμ.
x
νλ‘κ·Έλ¨ μ’
λ£
λκΈ