내적
i가 같을 때 arr1[i], arr2[i]를 곱한 값들을 더한 값
for
class Solution {
public int solution(int[] a, int[] b) {
int answer = 0;
for (int i = 0; i < a.length; i++) {
answer += a[i] * b[i];
}
return answer;
}
}
import java.util.stream.IntStream;
class Solution {
public int solution(int[] a, int[] b) {
return IntStream.range(0, a.length).map(index -> a[index] * b[index]).sum();
}
}
IntStram.range()에 대해 공부했었는데 써먹질 못했다.
앞서 filter에 대해 공부하며 언급했는데 바로 다음 문제에서 써먹을 수 있는 내용이 나왔다.
스트림 내 요소들을 하나씩 특정값으로 변환해준다.
사용법
ArrayList<string> list = new ArrayList<>(Arrays.asList("a", "aa", "aaa"));
list.stream().map(s -> s.toUpperCase()).forEach(s -> System.out.println(s));
.collect(Collectors.toList())
로 결과를 return 받거나 위의 예시처럼 forEach
로 출력할 수 있다.
A
AA
AAA