본문 바로가기

Windows 10/PowerShell

PowerShell : Array, 동적 배열

반응형

- 배열 선언

# 첫 번째 방법
$arr = 1, 2, 3, 4, 5

# 두 번째 방법
$arr = 1..5

# 세 번째 방법 (Size가 0인 Array)
$arr = @()

# 네 번째 방법 (동적 할당)
$arr = New-Object System.Collections.ArrayList
[void]$arr.Add(1)
$arr.Add(2)
[void]$arr.Add(3)
$arr.Add(4)
[void]$arr.Add(5)

<#
Add() 함수 사용시 Return되는 값이 자동으로 출력됨.
따라서 [void]를 앞에 넣음으로 Return값 미출력.
#>

- 배열 출력

$arr = 10, 20, 30, 40

# 첫 번째 방법
$arr

# 두 번째 방법
$arr[0..2]

# 세 번째 방법
for($i=0; $i -lt $arr.count; $i++)
{
    Write-Host $arr[$i]
}

# 네 번째 방법 (숫자 형식인 경우에만)
$arr.ForEach({$_ .. $_})

- 함수 (Size, Remove All Element)

$arr = 1..5

# 배열 길이 (Count와 Length는 Alias로 묶여 있어 같은 기능이다.)
$arr.Count
$arr.Length

# 모두 제거
$arr.Clear

 

 

about_Arrays

About Arrays In this article --> Short Description Describes arrays, which are data structures designed to store collections of items. Long Description An array is a data structure that is designed to store a collection of items. The items can be the same

docs.microsoft.com

 

반응형