๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- Java & kotlin

[Java] Arrays.copyOfRange ๋กœ ๋ฒ”์œ„ ๋งŒํผ ๋ฐฐ์—ด ๋ณต์‚ฌํ•˜๊ธฐ

by Wonit 2021. 7. 5.

์š”์ฆ˜ ํ•™๊ต ํ”„๋กœ์ ํŠธ๋ž‘ BoB ๋•Œ๋ฌธ์— ๋ฐ”๋น ์„œ ํ•œ ๋™์•ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฑฐ์˜ ๋†“๋‹ค์‹ถ์ด ํ•ด๋ฒ„๋ ธ๋‹ค..

 

๊ทธ๋ž˜์„œ ๊ฐ์„ ์ข€ ์ฐพ์œผ๋ ค๊ณ  ์˜ค๋žœ๋งŒ์— ๋‹ค์‹œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์— ๋“ค์–ด๊ฐ€์„œ Level 1 ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์žˆ์—ˆ๋‹ค.

 

๋ฌธ์ œ๋Š” k ๋ฒˆ์งธ ์ˆ˜ ๋ผ๋Š” ๋ฌธ์ œ์˜€์—ˆ๊ณ , ํฌ๊ฒŒ ์–ด๋ ต์ง€ ์•Š์€ ๋ฌธ์ œ์˜€๋‹ค.

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - K๋ฒˆ์งธ์ˆ˜

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

 

๋Œ€์ถฉ ๋ฐฐ์—ด ํ•˜๋‚˜๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” i, j ๊ฐ’์ด ์žˆ์œผ๋ฉด ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ i ~ j ๊นŒ์ง€ ํ•ด๋‹น๋˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด์—ˆ๋‹ค.

 

๋‚˜๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ Arrays ํด๋ž˜์Šค์˜ copyOfRange ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์ด๋ฅผ ํ•œ ์‚ฌ๋žŒ์ด ์žˆ์—ˆ๊ณ , copyOfRange ์— ํฅ๋ฏธ๊ฐ€ ์ƒ๊ฒจ ๋” ์•Œ์•„๋ณด๋ ค ํ•œ๋‹ค.

 

copyOfRange ๋กœ ๋ฐฐ์—ด ๋ณต์‚ฌํ•˜๊ธฐ

 

  • ์˜ค๋ผํด ๊ณต์‹ ๋ฌธ์„œ Arrays.class;

 

 

Arrays (Java Platform SE 7 )

Sorts the specified array into ascending numerical order. Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cau

docs.oracle.com

 

๊ณต์‹ ๋ฌธ์„œ์—์„œ copyOfRange ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•œ๋‹ค.

 

Copies the specified range of the specified array into a new array.

 

ํŠน์ • ๋ฒ”์œ„์— ๋”ฐ๋ผ์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };

        int[] refinedArray = Arrays.copyOfRange(array, 2, 6);

        System.out.println(Arrays.toString(refinedArray));
    }
}

 

Arrays.copyOfRange(์›๋ณธ ๋ฐฐ์—ด, ์‹œ์ž‘ ์ธ๋ฑ์Šค, ๋ ์ธ๋ฑ์Šค);

 

๊ทธ๋Ÿผ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ์›๋ณธ ๋ฐฐ์—ด๊ณผ ๋™์ผํ•œ ํƒ€์ž…์˜ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์‹ค์ œ ๊ตฌํ˜„์ด ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ๊ถ๊ธˆํ•ด์„œ ํ•œ ๋ฒˆ ์ฐพ์•„๋ณด๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

 

public static int[] copyOfRange(int[] original, int from, int to) {
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    int[] copy = new int[newLength];
    System.arraycopy(original, from, copy, 0,
                      Math.min(original.length - from, newLength));
    return copy;
}
  1. ๋ฐฐ์—ด์˜ size ๋ฅผ ๋ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ ์ธ๋ฑ์Šค ๋งŒํผ ์ฐจ๊ฐํ•œ ๊ฐ’์œผ๋กœ ์ƒˆ๋กญ๊ฒŒ ํ• ๋‹นํ•œ๋‹ค.
  2. ๋งŒ์•ฝ size ๊ฐ€ ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ, from ์ด to ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋Š” IllegalArgumentException ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  3. System ์˜ arraycopy ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณต์‚ฌ๋ณธ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

 

์—ฅ? System.arraycopy() ์— ๋Œ€ํ•ด์„œ ๋ด์•ผ๊ฒ ๋‹ค.

 

 

์Œ... ๊ทธ๋ƒฅ ๋ฐฐ์—ด์„ copy ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค..

 

์•„๋ฌดํŠผ ์ด๋ ‡๊ฒŒ ๋˜ ํ•˜๋‚˜์˜ ์Šคํ‚ฌ์„ ๋ฐฐ์› ๋‹ค!

๋Œ“๊ธ€