Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

리스트 n번째 원소까지 출력하기(자바) 본문

JavaAlgorithm

리스트 n번째 원소까지 출력하기(자바)

MyDiaryYo 2023. 8. 7. 18:27

정수리스트 num_list와 정수n이 주어질 때, num_list 첫번째 원소부터 n번째 원소까지 담은 리스트를 출력

 

방법1.

class Solution {
    public int[] solution(int[] num_list, int n) {
        int[] answer = new int[n];
        for(int i=0; i<n; i++){
            answer[i]=num_list[i];
        }
        return answer;
        
    }
    
}

 

 

방법2.

import java.util.Arrays;

class Solution {
    public int[] solution(int[] num_list, int n) {
       int[] answer = {};
       answer Arrays.copyOfRange(num, 0, n);
       return answer;
    }
}