https://earthteacher.tistory.com/169#gsc.tab=0
원시타입 배열을 리스트로
import java.util.*;
import java.util.stream.Collectors;
class Solution {
public int solution(int[] d, int budget) {
List<Integer> myList = Arrays.stream(d)
.boxed()
.collect(Collectors.toList());
배열 자르기
import java.util.Arrays; public class ArraySplit {
public static void main(String[] args) {
// 1. 원본 배열
int[] arr = {0, 1, 2, 3, 4, 5};
// 2. 배열을 자를 index
int position = 3;
// 3. 배열 자르기
int[] arr1 = Arrays.copyOfRange(arr, 0, position);
int[] arr2 = Arrays.copyOfRange(arr, position, arr.length);
// 4. 자른 배열 출력
System.out.println(Arrays.toString(arr1));
// [0, 1, 2]
System.out.println(Arrays.toString(arr2));
// [3, 4, 5]
}
}
'I leaned > JAVA' 카테고리의 다른 글
접근자 (0) | 2024.02.05 |
---|---|
ArrayList VS LinkedList (0) | 2024.01.29 |
상수는 왜 static final로 선언할까? (0) | 2023.10.05 |
record (0) | 2023.07.10 |