List

"Java Stuty"

Posted by Chungman on March 25, 2021

List 예제

ListMain

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
111
112
113
114
115
116
package Java0325.Lecture14.generic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/*
 *  List : 순서(o), 중복(o)
 *  구현클래스 : ArrayList, LinedList
 *  
 *  주요메소드 
 *  -add() : 데이터 입력
 *  -get() : 해당 인덱스에 있는 데이터 추출
 *  -remove() : 데이터 삭제
 *  -size() : 리스트의 전체 데이터 개수
 *  -clear() : 릿트의 전체 데이터 삭제
 *  -isEmpty() : 리스트가 비어있는지 채크
 *  -contains() : 특정데이터의 존재여부 판단
 *  -iterator() : Iterator 인터페이스 객체 반환
 */
public class ListMain {

	public static void main(String[] args) {
		
		/*
		List list = new ArrayList();	// 1.5 미만의 방식
		list.add("Hello");
		 */
		
//		List<String> list = new ArrayList<String>();	// 1.5버전의 Generic 사용
		List<String> list = new ArrayList<>();	// 1.7버전 이후 Generic 방식 
	
		System.out.println("리스트의 원소 총개수 : " + list.size());
		list.add("one");
		list.add("two");
		list.add("three");
		list.add("four");
		list.add("five");
		System.out.println("리스트의 원소 총개수 : " + list.size());
		list.add("one");	// 중복허용, boolean형이라서 원소가 들어오면 true 아니면 false로 인식
		System.out.println("리스트의 원소 총개수 : " + list.size());
		
//		list.add(10);	// The method add(int, String) in the type List<String> is not applicable for the arguments (int)
		
		/*
		 * list의 전체데이터 출력
		 * 1. index를 이용한 get()메소드 이용
		 * 2. 1.5버번의 for문 이용
		 * 3. toArray()를 이용
		 * 4. iterator()를 이용
		 */
		
		System.out.println("\n< get() 메소드를 이용한 출력 >");
		for(int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i) + "\t");
		}
	
		System.out.println("\n\n< 1.5버전의 for문을 이용한 출력 >");
		for(String str : list) {
			System.out.print(str + "\t");
		}
		
		System.out.println("\n\n< toArray() 메소드를 이용한 출력 >");
		Object[] arr = list.toArray();
		System.out.println(Arrays.toString(arr));
		
		/*
		 * Iterator(순환자) 주요 메소드
		 * 
		 * - next() : 데이터 추출
		 * - hasNext() : 추출할 다음데이터의 존재여부 판단 
		 */
		System.out.println("\n< iterator() 메소드를 이용한 출력 >");
		Iterator<String> ite = list.iterator();
		while(ite.hasNext()) {
			System.out.print(ite.next() + "\t");
		}
		
		String searchStr = "two";
		if(list.contains("two")) {
			System.out.println("\n"+searchStr + "은 존재합니다.");
		} else {
			System.out.println("\n"+searchStr + "은 존재하지 않습니다.");
		}
		
		boolean bool = list.remove("two");
		System.out.println("remove : " + bool);
		searchStr = "two";
		if(list.contains("two")) {
			System.out.println(searchStr + "은 존재합니다.");
		} else {
			System.out.println(searchStr + "은 존재하지 않습니다.");
		}
		bool = list.remove("two");
		System.out.println("remove : " + bool);
		
		System.out.println("첫번째 문자열 : "+list.get(0));
		String removeStr = list.remove(0);
		System.out.println("삭제된 문자열 : "+removeStr);
		System.out.println("remove(0) 후 첫번째 문자열 : " + list.get(0));
		
		System.out.println("원소 총개수 : "+ list.size());
//		list.clear();
//		System.out.println("원소 총개수 : "+ list.size());
		
		List<String> sub = new LinkedList<>();
		sub.add("봄");
		sub.add("여름");
		
		list.addAll(sub);
		System.out.println("addAll() 후 원소의 개수 : " + list.size());
	}

}