Generics
컬렉션(자료구조)
객체들을 저장(수집)하는 구조적인 성격을 보강하기 위해 제공되는 것
import static java.lang.System.out;
// static import문. System을 명시 안해도 사용 가능
class GenericEx1<T>{ // T는 미결정타입
T[] v;
public void set(T[] n){
v = n;
}
public void print(){
for(T s : v)
out.println(s);
}
}
Generic_class명<적용할_Generic_Type> 변수명; // 선언
변수명 = new Generic_class생성자명<적용할_Generic_Type>(); // 생성
public class GenericEx1Main {
public static void main(String[] args) {
GenericEx1<String> t = new GenericEx1<String>();
String[] ss = {"애","아","서"};
t.set(ss);
t.print();
// 좋은 방법이 아님
GenericEx1 t1 = new GenericEx1();
Integer[] s = {1,2,3};
t1.set(s);
t1.print();
}
}
set(집합) 성질을 가지고 있으면 중복된 값을 가질 수 없다. HashSet, TreeSet
list - Stack, Vector, ArrayList
Map - key(중복 불가)와 value로 이뤄져있는 자료구조 HashMap, TreeMap
반응형
'Java > java' 카테고리의 다른 글
GUI 프로그래밍 (0) | 2020.08.13 |
---|---|
예외처리 (Exception) (0) | 2020.08.12 |
내부 클래스 ( Inner Class ) (0) | 2020.08.11 |
인터페이스(Interface) (0) | 2020.08.11 |
추상화 (0) | 2020.08.11 |