ArrayList.IsFixedSize 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
ArrayList의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다.
public:
virtual property bool IsFixedSize { bool get(); };
public virtual bool IsFixedSize { get; }
member this.IsFixedSize : bool
Public Overridable ReadOnly Property IsFixedSize As Boolean
속성 값
true
가 고정 크기인 경우 ArrayList이고, 그렇지 않으면 false
입니다. 기본값은 false
입니다.
구현
예제
다음 코드 예제에서는 주위에 ArrayList고정 크기 래퍼를 만드는 방법을 보여줍니다.
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, char mySeparator );
int main()
{
// Creates and initializes a new ArrayList.
ArrayList^ myAL = gcnew ArrayList;
myAL->Add( "The" );
myAL->Add( "quick" );
myAL->Add( "brown" );
myAL->Add( "fox" );
myAL->Add( "jumps" );
myAL->Add( "over" );
myAL->Add( "the" );
myAL->Add( "lazy" );
myAL->Add( "dog" );
// Create a fixed-size wrapper around the ArrayList.
ArrayList^ myFixedSizeAL = ArrayList::FixedSize( myAL );
// Display whether the ArrayLists have a fixed size or not.
Console::WriteLine( "myAL {0}.", myAL->IsFixedSize ? (String^)"has a fixed size" : "does not have a fixed size" );
Console::WriteLine( "myFixedSizeAL {0}.", myFixedSizeAL->IsFixedSize ? (String^)"has a fixed size" : "does not have a fixed size" );
Console::WriteLine();
// Display both ArrayLists.
Console::WriteLine( "Initially," );
Console::Write( "Standard :" );
PrintValues( myAL, ' ' );
Console::Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
// Sort is allowed in the fixed-size ArrayList.
myFixedSizeAL->Sort();
// Display both ArrayLists.
Console::WriteLine( "After Sort," );
Console::Write( "Standard :" );
PrintValues( myAL, ' ' );
Console::Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
// Reverse is allowed in the fixed-size ArrayList.
myFixedSizeAL->Reverse();
// Display both ArrayLists.
Console::WriteLine( "After Reverse," );
Console::Write( "Standard :" );
PrintValues( myAL, ' ' );
Console::Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
// Add an element to the standard ArrayList.
myAL->Add( "AddMe" );
// Display both ArrayLists.
Console::WriteLine( "After adding to the standard ArrayList," );
Console::Write( "Standard :" );
PrintValues( myAL, ' ' );
Console::Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
Console::WriteLine();
// Adding or inserting elements to the fixed-size ArrayList throws an exception.
try
{
myFixedSizeAL->Add( "AddMe2" );
}
catch ( Exception^ myException )
{
Console::WriteLine( "Exception: {0}", myException );
}
try
{
myFixedSizeAL->Insert( 3, "InsertMe" );
}
catch ( Exception^ myException )
{
Console::WriteLine( "Exception: {0}", myException );
}
}
void PrintValues( IEnumerable^ myList, char mySeparator )
{
IEnumerator^ myEnum = myList->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::Write( "{0}{1}", mySeparator, obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
myAL does not have a fixed size.
myFixedSizeAL has a fixed size.
Initially,
Standard : The quick brown fox jumps over the lazy dog
Fixed size: The quick brown fox jumps over the lazy dog
After Sort,
Standard : brown dog fox jumps lazy over quick the The
Fixed size: brown dog fox jumps lazy over quick the The
After Reverse,
Standard : The the quick over lazy jumps fox dog brown
Fixed size: The the quick over lazy jumps fox dog brown
After adding to the standard ArrayList,
Standard : The the quick over lazy jumps fox dog brown AddMe
Fixed size: The the quick over lazy jumps fox dog brown AddMe
Exception: System.NotSupportedException: Collection was of a fixed size.
at System.Collections.FixedSizeArrayList.Add(Object obj)
at SamplesArrayList.Main()
Exception: System.NotSupportedException: Collection was of a fixed size.
at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
at SamplesArrayList.Main()
*/
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Create a fixed-size wrapper around the ArrayList.
ArrayList myFixedSizeAL = ArrayList.FixedSize( myAL );
// Display whether the ArrayLists have a fixed size or not.
Console.WriteLine( "myAL {0}.", myAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" );
Console.WriteLine( "myFixedSizeAL {0}.", myFixedSizeAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" );
Console.WriteLine();
// Display both ArrayLists.
Console.WriteLine( "Initially," );
Console.Write( "Standard :" );
PrintValues( myAL, ' ' );
Console.Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
// Sort is allowed in the fixed-size ArrayList.
myFixedSizeAL.Sort();
// Display both ArrayLists.
Console.WriteLine( "After Sort," );
Console.Write( "Standard :" );
PrintValues( myAL, ' ' );
Console.Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
// Reverse is allowed in the fixed-size ArrayList.
myFixedSizeAL.Reverse();
// Display both ArrayLists.
Console.WriteLine( "After Reverse," );
Console.Write( "Standard :" );
PrintValues( myAL, ' ' );
Console.Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
// Add an element to the standard ArrayList.
myAL.Add( "AddMe" );
// Display both ArrayLists.
Console.WriteLine( "After adding to the standard ArrayList," );
Console.Write( "Standard :" );
PrintValues( myAL, ' ' );
Console.Write( "Fixed size:" );
PrintValues( myFixedSizeAL, ' ' );
Console.WriteLine();
// Adding or inserting elements to the fixed-size ArrayList throws an exception.
try {
myFixedSizeAL.Add( "AddMe2" );
} catch ( Exception myException ) {
Console.WriteLine("Exception: " + myException.ToString());
}
try {
myFixedSizeAL.Insert( 3, "InsertMe" );
} catch ( Exception myException ) {
Console.WriteLine("Exception: " + myException.ToString());
}
}
public static void PrintValues( IEnumerable myList, char mySeparator ) {
foreach ( Object obj in myList )
Console.Write( "{0}{1}", mySeparator, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
myAL does not have a fixed size.
myFixedSizeAL has a fixed size.
Initially,
Standard : The quick brown fox jumps over the lazy dog
Fixed size: The quick brown fox jumps over the lazy dog
After Sort,
Standard : brown dog fox jumps lazy over quick the The
Fixed size: brown dog fox jumps lazy over quick the The
After Reverse,
Standard : The the quick over lazy jumps fox dog brown
Fixed size: The the quick over lazy jumps fox dog brown
After adding to the standard ArrayList,
Standard : The the quick over lazy jumps fox dog brown AddMe
Fixed size: The the quick over lazy jumps fox dog brown AddMe
Exception: System.NotSupportedException: Collection was of a fixed size.
at System.Collections.FixedSizeArrayList.Add(Object obj)
at SamplesArrayList.Main()
Exception: System.NotSupportedException: Collection was of a fixed size.
at System.Collections.FixedSizeArrayList.Insert(int index, Object obj)
at SamplesArrayList.Main()
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
' Create a fixed-size wrapper around the ArrayList.
Dim myFixedSizeAL As ArrayList = ArrayList.FixedSize(myAL)
' Display whether the ArrayLists have a fixed size or not.
Dim msg As String
If myAL.IsFixedSize Then
msg = "has a fixed size"
Else
msg = "does not have a fixed size"
End If
Console.WriteLine("myAL {0}.", msg)
If myFixedSizeAL.IsFixedSize Then
msg = "has a fixed size"
Else
msg = "does not have a fixed size"
End If
Console.WriteLine("myFixedSizeAL {0}.", msg)
Console.WriteLine()
' Display both ArrayLists.
Console.WriteLine("Initially,")
Console.Write("Standard :")
PrintValues(myAL, " "c)
Console.Write("Fixed size:")
PrintValues(myFixedSizeAL, " "c)
' Sort is allowed in the fixed-size ArrayList.
myFixedSizeAL.Sort()
' Display both ArrayLists.
Console.WriteLine("After Sort,")
Console.Write("Standard :")
PrintValues(myAL, " "c)
Console.Write("Fixed size:")
PrintValues(myFixedSizeAL, " "c)
' Reverse is allowed in the fixed-size ArrayList.
myFixedSizeAL.Reverse()
' Display both ArrayLists.
Console.WriteLine("After Reverse,")
Console.Write("Standard :")
PrintValues(myAL, " "c)
Console.Write("Fixed size:")
PrintValues(myFixedSizeAL, " "c)
' Add an element to the standard ArrayList.
myAL.Add("AddMe")
' Display both ArrayLists.
Console.WriteLine("After adding to the standard ArrayList,")
Console.Write("Standard :")
PrintValues(myAL, " "c)
Console.Write("Fixed size:")
PrintValues(myFixedSizeAL, " "c)
Console.WriteLine()
' Adding or inserting elements to the fixed-size ArrayList throws an exception.
Try
myFixedSizeAL.Add("AddMe2")
Catch myException As Exception
Console.WriteLine("Exception: " + myException.ToString())
End Try
Try
myFixedSizeAL.Insert(3, "InsertMe")
Catch myException As Exception
Console.WriteLine("Exception: " + myException.ToString())
End Try
End Sub
Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
Dim obj As [Object]
For Each obj In myList
Console.Write("{0}{1}", mySeparator, obj)
Next obj
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' myAL does not have a fixed size.
' myFixedSizeAL has a fixed size.
'
' Initially,
' Standard : The quick brown fox jumps over the lazy dog
' Fixed size: The quick brown fox jumps over the lazy dog
' After Sort,
' Standard : brown dog fox jumps lazy over quick the The
' Fixed size: brown dog fox jumps lazy over quick the The
' After Reverse,
' Standard : The the quick over lazy jumps fox dog brown
' Fixed size: The the quick over lazy jumps fox dog brown
' After adding to the standard ArrayList,
' Standard : The the quick over lazy jumps fox dog brown AddMe
' Fixed size: The the quick over lazy jumps fox dog brown AddMe
'
' Exception: System.NotSupportedException: Collection was of a fixed size.
' at System.Collections.FixedSizeArrayList.Add(Object obj)
' at SamplesArrayList.Main()
' Exception: System.NotSupportedException: Collection was of a fixed size.
' at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
' at SamplesArrayList.Main()
설명
크기가 고정된 컬렉션에서는 컬렉션을 만든 다음에 요소를 추가하거나 제거할 수 없지만 기존 요소는 수정할 수 있습니다.
고정된 크기의 컬렉션은 단순히 요소를 추가 및 제거하지 못하도록 하는 래퍼가 있는 컬렉션입니다. 따라서 요소 추가 또는 제거를 포함하여 기본 컬렉션이 변경되는 경우 고정 크기 컬렉션은 이러한 변경 내용을 반영합니다.
이 속성의 값을 검색하는 작업은 작업입니다 O(1)
.
적용 대상
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET