ArrayList Class
ArrayList 클래스의 도움말을 보면 아래와 같이 되어 있습니다.
필요에 따라 크기가 동적으로 증가하는 배열을 사용하여 IList 인터페이스를 구현합니다.
ArrayList 클래스 (System.Collections)
필요에 따라 크기가 동적으로 증가하는 배열을 사용하여 IList 인터페이스를 구현합니다.
learn.microsoft.com
아래 그림은 ChatGPT에게 질문하고 답변받은 내용입니다.
ArrayList는 참조를 한다고 해도 Intellisense를 사용할 수는 없으므로 이 글에서는 모두 Late Binding으로 작성합니다.
속성
Count property
ArrayList에 실제로 포함된 요소의 수를 가져옵니다.
Sub ArrayListExample()
' 새로운 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
' ArrayList에 있는 항목의 개수를 표시
Debug.Print "ArrayList에 있는 항목의 개수는: " & arrList.Count
End Sub
' 출력 결과
' ArrayList에 있는 항목의 개수는: 3
Item property
지정한 인덱스에 있는 요소를 가져오거나 설정합니다.
Sub TestArrayList()
Dim AL As Object
Set AL = CreateObject("System.Collections.ArrayList")
AL.Add "Apple"
AL.Add "Aratiles"
AL.Add "Bilberry"
AL.Add "Cacao"
AL.Add "Catmon"
AL.Add "Guava"
AL.Add "Longan"
Debug.Print "수정 전: " & AL.Item(2)
AL.Item(2) = "Blackberry"
Debug.Print "수정 후: " & AL.Item(2)
End Sub
' 출력 결과
' 수정 전: Bilberry
' 수정 후: Blackberry
메서드
Add method
ArrayList에 하나의 항목을 추가합니다.
Sub ArrayListAddExample()
' 새로운 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
' ArrayList에 있는 모든 항목을 표시
Dim i As Integer
For i = 0 To arrList.Count - 1
Debug.Print arrList(i)
Next i
End Sub
' 출력 결과
' Apple
' Banana
' Cherry
AddRange
ArrayList 의 끝에 여러 개의 요소를 추가합니다 .
Sub ArrayListAddRangeExample()
Dim arrList1 As Object
Dim arrList2 As Object
Set arrList1 = CreateObject("System.Collections.ArrayList")
Set arrList2 = CreateObject("System.Collections.ArrayList")
arrList1.Add "Apple"
arrList1.Add "Banana"
arrList1.Add "Cherry"
arrList2.AddRange arrList1
Dim i As Integer
For i = 0 To arrList2.Count - 1
Debug.Print arrList2(i)
Next i
End Sub
' 출력 결과
' Apple
' Banana
' Cherry
다음 코드는 모듈화된 코드입니다.
Sub ArrayListAddRangeExample()
' 첫 번째 ArrayList 생성 및 초기화
Dim arrList1 As Object
Set arrList1 = CreateObject("System.Collections.ArrayList")
AddFruitsToArrayList arrList1
' 두 번째 ArrayList 생성 및 첫 번째 리스트의 항목 복사
Dim arrList2 As Object
Set arrList2 = CreateObject("System.Collections.ArrayList")
CopyItemsBetweenArrayLists source:=arrList1, target:=arrList2
' 두 번째 리스트의 모든 항목 출력
PrintArrayListItems list:=arrList2
End Sub
' 특정 ArrayList에 과일 이름 추가
Sub AddFruitsToArrayList(ByRef list As Object)
list.Add "Apple"
list.Add "Banana"
list.Add "Cherry"
End Sub
' 한 ArrayList에서 다른 ArrayList로 항목 복사
Sub CopyItemsBetweenArrayLists(ByRef source As Object, ByRef target As Object)
target.AddRange source
End Sub
' 주어진 ArrayList의 모든 항목 출력
Sub PrintArrayListItems(ByRef list As Object)
Dim i As Integer
For i = 0 To list.Count - 1
Debug.Print list(i)
Next i
End Sub
Clear method
ArrayList 에서 모든 요소를 제거합니다.
Sub ArrayListClearExample()
' 새로운 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 몇 가지 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
' ArrayList의 내용 출력 (Apple, Banana, Cherry)
PrintArrayListItems list:=arrList
' ArrayList의 모든 항목 제거
arrList.Clear
' ArrayList의 내용 다시 출력 (아무것도 출력되지 않음)
PrintArrayListItems list:=arrList
End Sub
' 주어진 ArrayList의 모든 항목 출력
Sub PrintArrayListItems(ByRef list As Object)
Dim i As Integer
For i = 0 To list.Count - 1
Debug.Print list(i)
Next i
End Sub
Clone method
ArrayList의 얕은 복사본을 만듭니다.
Sub CloneExample()
' 첫 번째 ArrayList 생성 및 초기화
Dim arrList1 As Object
Set arrList1 = CreateObject("System.Collections.ArrayList")
arrList1.Add "Apple"
arrList1.Add "Banana"
arrList1.Add "Cherry"
' 첫 번째 ArrayList Clone하여 두 번째 ArrayList 생성
Dim arrList2 As Object
Set arrList2 = arrList1.Clone
' 첫 번째 ArrayList에서 항목 제거
arrList1.RemoveAt 0
' 각각의 list 출력 (arrList2에는 여전히 "Apple"이 있음)
Debug.Print "----- List 1 -----"
PrintArrayListItems list:=arrList1
Debug.Print "----- List 2 -----"
PrintArrayListItems list:=arrList2
End Sub
' 주어진 ArrayList의 모든 항목 출력
Sub PrintArrayListItems(ByRef list As Object)
Dim i As Integer
For i = 0 To list.Count - 1
Debug.Print list(i)
Next i
End Sub
' 출력결과
' ----- List 1 -----
' Banana
' Cherry
' ----- List 2 -----
' Apple
' Banana
' Cherry
Contains method
ArrayList에 요소가 있는지 확인합니다.
Sub ContainsExample()
' 새로운 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 몇 가지 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
' 특정 항목이 ArrayList에 있는지 확인
If arrList.Contains("Banana") Then
Debug.Print "Banana is in the list."
Else
Debug.Print "Banana is not in the list."
End If
End Sub
' 출력 결과
' Banana is in the list.
GetRange method
소스 ArrayList에서 요소의 하위 집합을 나타내는 ArrayList를 반환합니다.
Sub GetRangeExample()
' 새로운 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 몇 가지 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
arrList.Add "Durian"
' ArrayList의 일부 범위 가져오기
Dim subArrList As Object
Set subArrList = arrList.GetRange(1, 2) ' 인덱스 1에서 시작하여 2개의 항목을 가져옴
' subArrList 출력 (Banana, Cherry)
Dim i As Integer
For i = 0 To subArrList.Count - 1
Debug.Print subArrList(i)
Next i
End Sub
' 출력 결과
' Banana
' Cherry
IndexOf method
IndexOf 속성은 ArrayList에 있는 항목의 위치를 나타냅니다.
첫 번째 인수는 찾고 있는 값이고, 두 번째 인수는 값의 발생을 확인하려는 위치입니다.
Sub IndexOfExample()
' 새로운 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 몇 가지 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
' 특정 항목이 ArrayList에서 어느 위치에 있는지 확인
Dim index As Integer
index = arrList.IndexOf("Banana", 0)
If index <> -1 Then
Debug.Print "Banana is at position: " & (index + 1)
Else
Debug.Print "Banana is not in the list."
End If
End Sub
' 출력 결과
' Banana is at position: 2
여러 개의 항목이 있는 경우 모두 출력하는 코드입니다.
Sub ArrayListExample()
' 새로운 ArrayList 생성
Dim myAL As Object
Set myAL = CreateObject("System.Collections.ArrayList")
' ArrayList에 몇 가지 항목 추가
myAL.Add "Apple"
myAL.Add "Banana"
myAL.Add "Cherry"
myAL.Add "fox"
myAL.Add "Banana"
myAL.Add "Grape"
myAL.Add "Orange"
myAL.Add "Banana"
myAL.Add "Mango"
' 특정 항목이 ArrayList에서 어느 위치에 있는지 모두 확인
Dim i As Integer: i = 0
Do While i < myAL.Count
i = myAL.IndexOf("Banana", i)
If i <> -1 Then
Debug.Print "'Banana' is at position: " & (i + 1) ' 1-based index for readability
i = i + 1 ' Move to the next position after found item for next search.
Else
Exit Do ' No more Banana found, exit the loop.
End If
Loop
End Sub
' 출력 결과
' Banana' is at position: 2
' Banana' is at position: 5
' Banana' is at position: 8
Insert method
ArrayList의 지정된 인덱스에 요소를 삽입합니다.
Sub InsertIntoArrayList()
Dim AL As Object
Dim Item As Variant
Dim insertIndex As Integer
Dim insertElement As String
Set AL = CreateObject("System.Collections.ArrayList")
' 요소 추가
AL.Add ("Apple")
AL.Add ("Banana")
AL.Add ("Cherry")
' 삽입할 위치와 요소 지정
insertIndex = 1
insertElement = "Orange"
' 요소 삽입
AL.Insert insertIndex, insertElement
' ArrayList 출력
For Each Item In AL
Debug.Print Item
Next
End Sub
'출력 결과
' Apple
' Orange
' Banana
' Cherry
InsertRange method
ArrayList의 지정된 인덱스에 컬렉션의 요소를 삽입합니다.
Sub TestInsertRange()
Dim AL1 As Object
Dim AL2 As Object
Dim Item As Variant
Set AL1 = CreateObject("System.Collections.ArrayList")
Set AL2 = CreateObject("System.Collections.ArrayList")
AL1.Add "Apple"
AL1.Add "Apricot"
AL1.Add "Banana"
AL1.Add "Cherry"
AL1.Add "Grape"
PrintArrayList AL1
AL2.Add "Cacao"
AL2.Add "Guava"
AL2.Add "Jambul"
AL2.Add "Lime"
AL2.Add "Mango"
AL1.InsertRange 2, AL2
PrintArrayList AL1
End Sub
Sub PrintArrayList(obj As Object)
Dim var As Variant
For Each var In obj
Debug.Print var
Next
Debug.Print String(30, "-")
End Sub
' 출력 결과
' Apple
' Apricot
' Banana
' Cherry
' Grape
' ------------------------------
' Apple
' Apricot
' Cacao
' Guava
' Jambul
' Lime
' Mango
' Banana
' Cherry
' Grape
' ------------------------------
LastIndexOf method
ArrayList 에서 마지막으로 나오는 값의 인덱스를 반환합니다.
만약 해당 항목이 ArrayList에 없다면, 이 메서드는 -1을 반환합니다.
Sub TestLastIndexOf()
Dim AL As Object
Dim lastIndex As Integer
Set AL = CreateObject("System.Collections.ArrayList")
AL.Add "Apple"
AL.Add "Bilberry"
AL.Add "Cacao"
AL.Add "Apple"
AL.Add "Guava"
AL.Add "Cacao"
lastIndex = AL.LastIndexOf("Apple")
Debug.Print "마지막 'Apple'의 위치: " & lastIndex
lastIndex = AL.LastIndexOf("Cacao")
Debug.Print "마지막 'Cacao'의 위치: " & lastIndex
lastIndex = AL.LastIndexOf("Grape")
Debug.Print "마지막 'Grape'의 위치: " & lastIndex
End Sub
' 출력 결과
' 마지막 'Apple'의 위치: 3
' 마지막 'Cacao'의 위치: 5
' 마지막 'Grape'의 위치: -1
Remove method
ArrayList에서 맨 처음 발견되는 특정 개체를 제거합니다.
다음은 Remove method를 사용하는 간단한 예제입니다.
Sub RemoveFromArrayList()
' 새 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
Debug.Print "제거 전: "
' ArrayList의 각 항목 출력.
Dim item As Variant
For Each item In arrList
Debug.Print item
Next
' 리스트에서 두 번째 항목 ("Banana") 제거.
arrList.Remove "Banana"
Debug.Print vbNewLine & "제거 후: "
For Each item In arrList
Debug.Print item
Next
End Sub
' 출력 결과
' 제거 전:
' Apple
' Banana
' Cherry
' 제거 후:
' Apple
' Cherry
RemoveAt method
ArrayList의 지정된 인덱스에 있는 요소를 제거합니다.
인덱스는 0부터 시작합니다.
다음 코드는 RemoveAt method를 사용하여 두 번째 항목을 제거하는 예제입니다.
Sub RemoveAtFromArrayList()
' 새 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
Debug.Print "제거 전: "
' ArrayList의 각 항목 출력.
Dim item As Variant
For Each item In arrList
Debug.Print item
Next
' 리스트에서 두 번째 항목 ("Banana") 제거.
arrList.RemoveAt 1
Debug.Print vbNewLine & "제거 후: "
For Each item In arrList
Debug.Print item
Next
End Sub
'출력 결과
' 제거 전:
' Apple
' Banana
' Cherry
' 제거 후:
' Apple
' Cherry
RemoveRange method
ArrayList에서 요소의 범위를 제거합니다.
Syntex
object.RemoveRange (index, count)
Paramters
index : 제거할 요소의 범위에 대한 0부터 시작하는 인덱스입니다.
Count : 제거할 요소의 수입니다.
다음 코드는 세 번째 항목부터 4개의 항목을 제거합니다.
Sub RemoveRangeFromArrayList()
' 새 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
arrList.Add "Damson"
arrList.Add "Elderberry"
arrList.Add "Grape"
arrList.Add "Guava"
arrList.Add "Jambul"
arrList.Add "Lemon"
arrList.Add "Longan"
Debug.Print "Before removal: "
' ArrayList의 각 항목 출력
Dim item As Variant
For Each item In arrList
Debug.Print item
Next
' 인덱스 2에서 시작하여 4개의 항목을 제거
arrList.RemoveRange 2, 4
Debug.Print vbNewLine & "After removal: "
For Each item In arrList
Debug.Print item
Next
End Sub
' 출력 결과
' Before removal:
' Apple
' Banana
' Cherry
' Damson
' Elderberry
' Grape
' Guava
' Jambul
' Lemon
' Longan
' After removal:
' Apple
' Banana
' Guava
' Jambul
' Lemon
' Longan
Reverse method
ArrayList의 요소 순서를 반대로 바꿉니다.
다음 코드는 역순으로 정렬하는 간단한 코드입니다.
Sub ReverseArrayList()
' 새 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
arrList.Add "Damson"
arrList.Add "Elderberry"
arrList.Add "Grape"
arrList.Add "Guava"
arrList.Add "Jambul"
arrList.Add "Lemon"
arrList.Add "Longan"
Debug.Print "역순으로 정렬하기 전"
' ArrayList의 각 항목 출력
Dim item As Variant
For Each item In arrList
Debug.Print item
Next
' ArrayList 역순 정렬
arrList.Reverse
Debug.Print vbNewLine & "역순 후: "
For Each item In arrList
Debug.Print item
Next
End Sub
' 출력 결과
' 역순으로 정렬하기 전
' Apple
' Banana
' Cherry
' Damson
' Elderberry
' Grape
' Guava
' Jambul
' Lemon
' Longan
' 역순 후:
' Longan
' Lemon
' Jambul
' Guava
' Grape
' Elderberry
' Damson
' Cherry
' Banana
' Apple
Sort method
ArrayList의 요소를 정렬합니다.
Sub SortArrayList()
' 새 ArrayList 생성
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
' ArrayList에 항목 추가
arrList.Add "Cherry"
arrList.Add "Apple"
arrList.Add "Grape"
arrList.Add "Damson"
arrList.Add "Elderberry"
arrList.Add "Lemon"
arrList.Add "Guava"
arrList.Add "Jambul"
arrList.Add "Banana"
arrList.Add "Longan"
Debug.Print "정렬 전: "
' ArrayList의 각 항목 출력
Dim item As Variant
For Each item In arrList
Debug.Print item
Next
' ArrayList 정렬
arrList.Sort
Debug.Print vbNewLine & "정렬 후: "
For Each item In arrList
Debug.Print item
Next
End Sub
' 출력 결과
' 정렬 전:
' Cherry
' Apple
' Grape
' Damson
' Elderberry
' Lemon
' Guava
' Jambul
' Banana
' Longan
' 정렬 후:
' Apple
' Banana
' Cherry
' Damson
' Elderberry
' Grape
' Guava
' Jambul
' Lemon
' Longan
ToArray method
ArrayList의 요소를 새 배열에 복사합니다.
다음 코드는 ToArray 메서드를 사용하여 배열에 복사한 후 시트에 출력하는 간단한 예제입니다.
Sub Main()
' ArrayList 생성 및 데이터 추가
Dim arrList As Object
Set arrList = CreateArrayList()
' ArrayList를 배열로 변환
Dim myArray() As Variant
myArray = ConvertToArray(arrList)
' 시트에 출력
PrintToSheet myArray, Range("A1")
End Sub
Function CreateArrayList() As Object
' 새 ArrayList 생성 및 데이터 추가
Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList")
arrList.Add "Apple"
arrList.Add "Banana"
arrList.Add "Cherry"
arrList.Add "Damson"
arrList.Add "Elderberry"
arrList.Add "Grape"
arrList.Add "Guava"
arrList.Add "Jambul"
arrList.Add "Lemon"
arrList.Add "Longan"
Set CreateArrayList = arrList
End Function
Function ConvertToArray(arrObj As Object) As Variant
' ArrayList를 배열로 변환
ConvertToArray = arrObj.ToArray
End Function
Sub PrintToSheet(myArr() As Variant, startCell As Range)
startCell.Resize(UBound(myArr) - LBound(myArr) + 1, 1) = Application.Transpose(myArr)
End Sub
도움말 출처
ArrayList 클래스 (System.Collections)
필요에 따라 크기가 동적으로 증가하는 배열을 사용하여 IList 인터페이스를 구현합니다.
learn.microsoft.com
'외부 라이브러리' 카테고리의 다른 글
SortedList (0) | 2023.10.03 |
---|