Day 16(8/22) ArrayList, HashMap, HashSet
2016. 8. 23. 17:37ㆍProgramming/Java
**오늘 배운 내용**
1) ArrayList 인자 추가 및 출력
2) HashMap 저장 및 출력
3) HashSet은 중복 저장 불가(put도)
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.ArrayList;//제네릭을 가진다.
import java.util.List;//ArrayList를 구현하기 위한 interface(설계도)
public class L01ArrayList {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();//아무것도 쓰지 않으면 generic은 Object
list.add("월");
list.add("화");
list.add("수");
//list.add(4); <String> => String만 받음
//list.add(5.0);
//list.add('토');
//list.add("일");
String[] str_list = {"월", "화", "수", "목", "금", "토", "일"};
//배열(array)는 vs. ArrayList는
//1. 데이터 타입이 한정적이다. => generic으로 유동적
//2. 초기 index가 고정이다. => .add() 계속 추가 가능
//3. index를 가진다. => size()를 가진다.
//4. 프로그래머가 출력 => syso()로 출력 가능
System.out.println(list);
System.out.println(str_list);//배열의 주소만 출력
for(String e: str_list){
System.out.print(e+",");
}
for(int i =0; i<list.size(); i++){
System.out.print(list.get(i)+",");
}
}
} |
cs |
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 |
public class L02HashMap {
public static void main(String[] args) {
Map map = new HashMap();
map.put("id", "kimg");
map.put("name", "gowoon");
map.put("gender", "male");
map.put("age", 26);
System.out.println(map.toString());
//{gender=male, name=gowoon, id=kimg, age=26} 출력됨
System.out.println(map.get("id"));
//kimg 출력됨
Set<Map.Entry<String,Object>> entries = map.entrySet();
Iterator<Map.Entry<String, Object>> it = entries.iterator();
//Iterator 사용시
while(it.hasNext()){
Map.Entry<String,Object> entry = it.next();
System.out.println(entry.getKey()+"="+entry.getValue()+",");
}
//Foreach 사용시
for(Map.Entry<String, Object> entry: entries){
System.out.println(entry.getKey()+"="+entry.getValue()+",");
}
//List 내용을 Map에 넣기
List fish = new ArrayList();
fish.add("삼치");
fish.add("고등어");
Map food = new HashMap();
food.put("물고기",fish);
food.putAll(map);
System.out.println(food.get("물고기"));
//[삼치, 고등어]가 출력됨
System.out.println(food);
//{gender=male, 물고기=[삼치, 고등어], name=gowoon, id=kimg, age=26}
}
} |
cs |
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 |
public class L03HashSet {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("one");
set.add("two");
set.add("three");
set.add("three");
System.out.println(set.size());
Set<Integer> a = new HashSet<Integer>();
a.add(1);a.add(2);a.add(3);
Set<Integer> b = new HashSet<Integer>();
b.add(3);b.add(4);b.add(5);
Set<Integer> c = new HashSet<Integer>();
c.add(1);c.add(2);
//a가 c를 모두 포함하나?
a.containsAll(c);
//a와 c의 교집합
a.retainAll(c);
//a와 b의 합집합
a.addAll(b);
//a에서 c를 빼기
a.removeAll(c);
}
} |
cs |
'Programming > Java' 카테고리의 다른 글
Day 18(8/25) 숫자 맞추기 게임, 가위바위보 (0) | 2016.08.26 |
---|---|
Day 17(8/24) Try~Catch, throws (0) | 2016.08.26 |
Day 15(8/19) Generic (0) | 2016.08.23 |
Day 14(8/18) Final, Enum (0) | 2016.08.23 |
Day 13(8/17) public, private (0) | 2016.08.23 |