문제
다음 Java 코드의 실행 결과를 쓰시오.
Javaimport java.util.*; public class Main { public static void main(String[] args) { List<String> words = new ArrayList<>(Arrays.asList("cat", "elephant", "dog")); words.sort((x, y) -> Integer.compare(y.length(), x.length())); System.out.println(words.get(1) + " " + words.get(0)); } }
정답
cat elephant
cat elephant
해설
길이 내림차순 정렬을 수행합니다. elephant(8글자), cat(3글자), dog(3글자) 순으로 정렬됩니다. words.get(1)은 두 번째 요소인 "cat", words.get(0)은 첫 번째 요소인 "elephant"를 반환하므로 "cat elephant"가 출력됩니다.