Advanced Classroom
2013.09.17 09:42

나뚜루's - 배열, 이렇게도 만들 수 있다.

Views 2460 Votes 0 Comment 2
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print Update Delete
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print Update Delete
흔히 배열을 만들때는 이렇게 하죠.

array = []
ary = [1, 2, 3, 10]

하지만 배열도 일종의 클래스라서, 이렇게 만들 수 있습니다.

array = Array.new

그런데 그냥 이렇게만 만들 수 있는걸까요?
절대 아닙니다. Array 클래스의 initialize 함수에는 놀라운 사실이 숨어있습니다.

ary = Array.new(1, 10)

이게 뭘까요? 잘 이해가 안가실겁니다.

p ary # => [10]

[10]이라는 배열이 작성되었네요!
그렇다면 이렇게 하면 어떻게 될까요?

ary = Array.new(3, 5)
p ary # => [5, 5, 5]

이제 이해하셨을겁니다.

array = Array.new(갯수, 값)

이렇게 만드시면 갯수만큼 값이 배열 안에 들어가게됩니다. (값은 모두 똑같습니다.)
이것은 for 루프를 없애주기 때문에 똑같은 값을 여러번 만들 때는 정말 유용한 방법입니다.
하지만 결정적인 단점이 있죠. 모두 똑같은 값이기 때문에 하나를 변경하면 모두 변경됩니다.
예제를 보여드리겠습니다.

ary = Array.new(3, 'A')
ary[0] += '2'
p ary # => ['A2', 'A2', 'A2']

하지만 숫자는 다릅니다.

ary = Array(Array.new(3, 1)
ary[0] += 10
p ary # => [11, 1, 1]

숫자는 사칙연산을 하면 또다른 값을 만들어 돌려주기 때문에 이런 일이 일어나지 않습니다.
비록 복제하지 않으면 숫자에서만 가능하다는 점이 있지만, 그래도 알아두면 유용하지 않을까요?
?

  1. 게임제작강좌 A-Z 색인 (2016.1.24 ver)

    Date2016.01.12 CategoryBeginner Classroom By천무 Views8249 Votes1
    read more
  2. [야매 알만툴 MV 강좌] - 연출과 기능. 두마리 토끼를 동시에.

    Date2018.09.10 CategoryBeginner Classroom By케이디 Views3005 Votes0
    Read More
  3. RPG Maker MV에서 도트 느낌의 폰트 사용하기

    Date2018.07.15 CategoryBeginner Classroom By러닝은빛 Views5170 Votes1
    Read More
  4. [ MV ] 1-1 " RPG MAKER MV에 대하여 알아보자! "

    Date2018.07.01 CategoryBeginner Classroom By수성의물 Views2098 Votes0
    Read More
  5. [DLC 매뉴얼] GENE 도움말 한글화 Part.3 이용규약

    Date2018.04.25 CategoryBeginner Classroom By이니군 Views1057 Votes0
    Read More
  6. [DLC 매뉴얼] GENE 도움말 한글화 Part.2 이벤트 타입과 메뉴 바 컨텐츠

    Date2018.04.25 CategoryBeginner Classroom By이니군 Views1071 Votes0
    Read More
  7. [DLC 매뉴얼] GENE 도움말 한글화 Part.1 씬 빌더의 개요와 씬빌더 시작하기

    Date2018.04.24 CategoryBeginner Classroom By이니군 Views2442 Votes1
    Read More
  8. 느무느무 쉬운 XAS강좌!(VXA용)1.다운로드 및 규정.

    Date2017.11.28 CategoryBeginner Classroom By약빨은ㄱㅏㅅㅣ Views1760 Votes0
    Read More
  9. 저 같은 사람 생기지 말라고 만드는 XAS(액션알피지[액알])강 좌!(VX ACE용)

    Date2017.11.28 CategoryBeginner Classroom By약빨은ㄱㅏㅅㅣ Views1575 Votes0
    Read More
  10. [MV] Iavra Splash Video(스플래시/인트로영상 제작 시 스킵기능 부여 플러그인)

    Date2017.09.11 CategoryBeginner Classroom By이니군 Views1555 Votes0
    Read More
  11. RPG MAKER MV Tool Program-MADO 설치 및 적용기

    Date2017.09.08 CategoryBeginner Classroom By이니군 Views2662 Votes0
    Read More
  12. 커먼이벤트로 은행시스템 만들기(23:00 그림 링크 재수정)

    Date2017.08.28 CategoryBeginner Classroom By이니군 Views1890 Votes0
    Read More
  13. [RMMV] 크로노 엔진(Chrono Engine) 플러그인 사용법

    Date2017.07.16 CategoryAdvanced Classroom By러닝은빛 Views4069 Votes2
    Read More
  14. 네코플에서 VXA 선택지 잘리는 버그 해결하는법

    Date2017.06.22 CategoryIntermediate Classroom By심심치 Views1325 Votes2
    Read More
  15. 변수를 이용한 식물 재배

    Date2017.05.28 CategoryBeginner Classroom ByMoonJ Views3615 Votes2
    Read More
  16. 누구나 쉽게 따라할 수 있는 액션/슈팅 이벤트 만들기!

    Date2017.01.26 CategoryBeginner Classroom By이제리아 Views4762 Votes3
    Read More
  17. RPG란 무엇인가?가 하는 강의-1편:대쉬 속도와 걷는 속도를 어떻게 바꾸는가?-

    Date2016.10.31 CategoryIntermediate Classroom ByRPG란무엇인가? Views2591 Votes0
    Read More
  18. 쯔꾸르 게임을 네코플레이어에 최적화 시켜보자! # 해상도 편

    Date2016.04.17 CategoryIntermediate Classroom By양갱님 Views36610 Votes10
    Read More
  19. 게임의 퀼리티를 높이자! 3편-맵배치

    Date2016.04.08 CategoryBeginner Classroom Byifeve Views5592 Votes2
    Read More
  20. Input 객체의 키코드 확장

    Date2016.04.05 CategoryBeginner Classroom By러닝은빛 Views1722 Votes0
    Read More
  21. 초보가쓰는 초보용 Rpg게임 만들기 (xp)

    Date2016.03.13 CategoryBeginner Classroom ByMarine Views6522 Votes0
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 Next
/ 10


[privacy statements] | [Terms of Use] | [Contact us] | [Sponsorship] | [Indiside History]

Copyright © 1999 - 2016 INdiSide.com/CL3D Co., Ltd. All Rights Reserved.
Owner : Chunmu(Jiseon Lee) | kernys(Wonbae Kim) | Sasinji(Byungkook Kim)