-
배열 형변환 (Stream)코딩 테스트 2023. 1. 17. 16:41
백준 문제를 풀다가 String[] 을 Integer[]로 형변환하는게 많이 필요했다.
그런데 형변환을 할 때마다 for 문을 쓰면 코드가 너무 더러워 보여서 아래와 같은 방법을 썼다.
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine()); List<Integer[]> list = new ArrayList<Integer[]>(); for (int i = 0; i < num; i++) { String s = br.readLine(); String[] tmp = s.split(" "); // String타입인 tmp를 Integer로 형변환 Integer[] len = Stream.of(tmp).mapToInt(Integer::parseInt).boxed().toArray(Integer[]::new); list.add(len); }
'코딩 테스트' 카테고리의 다른 글
[백준] 1929번 소수 구하기 (0) 2023.01.19 [백준] 2675번 문자열 반복 - 브론즈 2 (0) 2023.01.17 [백준] 2563번 색종이 - 실버5 (0) 2023.01.17 [백준] 20115번 에너지 드링크(그리디) - 실버3 (0) 2023.01.15 [백준] 1978번 소수 찾기 - 실버 5 (0) 2023.01.14