Skip to main content

[Java] String[] cannot be converted to List<String>

❌ String[] cannot be converted to List<String> 에러

image.png

/SolutionTest.java:46: error: incompatible types: String[] cannot be converted to List<String>
		runner.run(() -> sol.solution(p0_0));
		                              ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

sol.solution(p0_0)에서 p0_0String[] 타입인데, solution() 메서드는 List<String> 타입을 매개변수로 기대하고 있다는 뜻이다. String[] 배열을 List<String>으로 변환하면 되고 Arrays.asList() 로 감싸서 해결한다.

runner.run(() -> sol.solution(Arrays.asList(p0_0)));

단, 이때 p0_0String[] 타입이어야 하며 import java.util.Arrays;가 필요하다.

현재 상황

String[] p0_0 = {"u", "d", "l", "r"};
runner.run(() -> sol.solution(p0_0));  // ❌ 오류

수정 코드

  • Arrays.asList()읽기 전용 고정 리스트를 반환하므로, 요소 추가/삭제는 안 된다.
  • 그게 필요하면 new ArrayList<>(Arrays.asList(...))로 감싸야 한다.
import java.util.Arrays;

String[] p0_0 = {"u", "d", "l", "r"};
runner.run(() -> sol.solution(Arrays.asList(p0_0)));  // ✅ 해결