item 32) 제네릭과 가변 인수를 함께 사용
가변 인수의 실수
제네릭과 가변 인수
static void dangerous(List<String>... stringLists) {
List<Integer> intList = List.of(3);
Object[] objects = stringLists;//제네릭 배열으로 가변 매개변수를 받을 수 있다.
objects[0] = intList;// 매개변수 배열에 무언가를 저장하여 힙 오염이 발생한다.
String s = stringLists[0].get(0);// ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String
}@SafeVarargs
타입 안전성을 깨는 경우
제네릭 varargs 매개변수를 안전하게 사용하는 방법
List매개변수
Last updated