๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[JAVA] List์—์„œ ๋ฐฐ์—ด(Array)๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ๋ณธ๋ฌธ

Question

[JAVA] List์—์„œ ๋ฐฐ์—ด(Array)๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

Shinjio 2023. 8. 24. 09:12

List.toArray()

- ๊ธฐ๋ณธ ํƒ€์ž…์˜ ์ž๋ฃŒํ˜• (ex. int[])๋กœ๋Š” ๋ณ€ํ™˜ ๋ถˆ๊ฐ€๋Šฅ

 

Integer[] answer = arr.toArray(new Integer[arr.size()]);

 

    public Integer[] solution(int[] numbers) {
        
        ArrayList<Integer> arr = new ArrayList<Integer>();
        
        int idx = 0;
        for(int i = 0 ; i < numbers.length ; i++){
            for(int j = i + 1 ; j < numbers.length ; j++){
                int sum = numbers[i] + numbers[j];
            
                if(!arr.contains(sum)) {
                	arr.add(sum);
                };
            }
        }
        arr.sort(Comparator.naturalOrder());
        
        Integer[] answer = arr.toArray(new Integer[arr.size()]);
        
       
        return answer;
    }