본문 바로가기

Language/Java10

지네릭스 지네릭스란 컴파일시 타입을 체크해주는 기능이다. ArrayList는 모든 종류의 객체가 저장가능한데 타입을 지정해주고 객체를 생성해주면 지정한 타입의 객체만 생성할 수 있게된다. import java.util.ArrayList; public class GenericTest { public static void main(String[] args){ ArrayList list = new ArrayList(); list.add(10); list.add(20); list.add("30"); //String추가 Integer i = (Integer)list.get(2); //컴파일 OK System.out.println(list); } } 이렇게 실행했을 시 ClassCastException(형변환에러, 실행시 .. 2022. 1. 16.
Collections 컬렉션을 다룰때 필요한 유용한 메서드들(static)을 제공한다. 1. 컬렉션 채우기, 복사, 정렬, 검색 : 2. 컬렉션의 동기화 : synchronized~() static Collections synchronizedCollection(Collection c) static List synchronizedList(List list) static Set synchronizedSet(Set s) static Map synchronizedMap(Map m) static SortedSet synchronizedSortedSet(SortedSet s) static SortedMap synchronizedSortedMap(SortedMap m) 3. 변경불가 컬렉션 만들기 : unmodifiable~() 4. 싱.. 2022. 1. 14.
Arrays 배열을 다루기 편리한 메서드를 제공한다. 참고로 Objects, Collections가 static메서드를 제공하는 util 패키지는 유용한 메서드를 제공하는 클래스를 제공한다. Arrays 배열의 출력 3. 배열 채우기 : fill(), setAll() 4. 배열의 정렬과 검색 : 이진탐색은 정렬된 배열에만 가능하다. 그렇지 않으면 잘못된 결과가 나온다. 정렬 후 검색을 해야한다. int[] arr = {3, 2, 0, 1, 4}; int idx = Arrays.binarySearch(arr, 2); //잘못된 결과 Arrays.sort(arr); //배열 arr을 정렬한다 System.out.println(Arrays.toString(arr)); int idx = Arrays.binarySearch(.. 2022. 1. 11.
Iterator, ListIterator, Enumeration - 컬렉션에 저장된 데이터를 접근하는데 사용되는 인터페이스이다. - Enumeration은 Iterator의 구버전이다. - ListIterator는 Iterator의 접근성을 향상시켜서 next()와 previous()가 있어 다음 요소와 이전 요소를 읽어 올 수 있다. List인터페이스를 구현한 컬렉션에서만 사용가능하다. 메서드 설명 boolean hasNext() 읽어 올 요소가 남아있는지 확인. 있으면 true, 없으면 false를 반환 Object next() 다음 요소를 읽어옴. next()를 호출하기 전에 hasNext()를 호출해서 읽어올 요소가 있는지 확인하는 것이 안전함 void remove() next()로 읽어 온 요소를 삭제. next()를 호출한 다음에 remove()를 호출해야.. 2022. 1. 11.