다음을 통해 공유


ArrayList.IsFixedSize 속성

ArrayList의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다.

네임스페이스: System.Collections
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Overridable ReadOnly Property IsFixedSize As Boolean
‘사용 방법
Dim instance As ArrayList
Dim value As Boolean

value = instance.IsFixedSize
public virtual bool IsFixedSize { get; }
public:
virtual property bool IsFixedSize {
    bool get ();
}
/** @property */
public boolean get_IsFixedSize ()
public function get IsFixedSize () : boolean

속성 값

ArrayList의 크기가 고정되어 있으면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

설명

고정 크기의 컬렉션에서는 컬렉션이 만들어진 후에 요소를 추가하거나 제거할 수 없지만 기존 요소를 수정할 수는 있습니다.

크기가 고정된 컬렉션이란 간단히 말해 요소가 추가되거나 제거되는 것을 방지하는 래퍼가 있는 컬렉션입니다. 따라서 요소를 추가하거나 제거하는 등 내부 컬렉션이 변경되면 크기가 고정된 컬렉션에 이러한 변경 사항이 반영됩니다.

이 속성의 값을 검색하는 것은 O(1) 연산입니다.

예제

다음 코드 예제에서는 ArrayList에 대한 고정 크기의 래퍼를 만드는 방법을 보여 줍니다.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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("jumped")
        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 'Main

    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 'PrintValues

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 jumped over the lazy dog
' Fixed size: The quick brown fox jumped over the lazy dog
' After Sort,
' Standard  : brown dog fox jumped lazy over quick the The
' Fixed size: brown dog fox jumped lazy over quick the The
' After Reverse,
' Standard  : The the quick over lazy jumped fox dog brown
' Fixed size: The the quick over lazy jumped fox dog brown
' After adding to the standard ArrayList,
' Standard  : The the quick over lazy jumped fox dog brown AddMe
' Fixed size: The the quick over lazy jumped 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( "jumped" );
      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 jumped over the lazy dog
Fixed size: The quick brown fox jumped over the lazy dog
After Sort,
Standard  : brown dog fox jumped lazy over quick the The
Fixed size: brown dog fox jumped lazy over quick the The
After Reverse,
Standard  : The the quick over lazy jumped fox dog brown
Fixed size: The the quick over lazy jumped fox dog brown
After adding to the standard ArrayList,
Standard  : The the quick over lazy jumped fox dog brown AddMe
Fixed size: The the quick over lazy jumped 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 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( "jumped" );
   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 jumped over the lazy dog
 Fixed size: The quick brown fox jumped over the lazy dog
 After Sort,
 Standard  : brown dog fox jumped lazy over quick the The
 Fixed size: brown dog fox jumped lazy over quick the The
 After Reverse,
 Standard  : The the quick over lazy jumped fox dog brown
 Fixed size: The the quick over lazy jumped fox dog brown
 After adding to the standard ArrayList,
 Standard  : The the quick over lazy jumped fox dog brown AddMe
 Fixed size: The the quick over lazy jumped 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()

 */
import System.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[] args)
    {
        // Creates and initializes a new ArrayList.
        ArrayList myAL = new ArrayList();

        myAL.Add("The");
        myAL.Add("quick");
        myAL.Add("brown");
        myAL.Add("fox");
        myAL.Add("jumped");
        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.get_IsFixedSize()
            ? "has a fixed size" : "does not have a fixed size");
        Console.WriteLine("myFixedSizeAL {0}.", myFixedSizeAL.get_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 (System.Exception myException) {
            Console.WriteLine("Exception: " + myException.ToString());
        }
        try {
            myFixedSizeAL.Insert(3, "InsertMe");
        }
        catch (System.Exception myException) {
            Console.WriteLine("Exception: " + myException.ToString());
        }
    } //main

    public static void PrintValues(IEnumerable myList, char mySeparator)
    {
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("{0}{1}", new Character(mySeparator), obj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArrayList 
/*
 This code produces the following output.
 
 myAL does not have a fixed size.
 myFixedSizeAL has a fixed size.

 Initially,
 Standard  : The quick brown fox jumped over the lazy dog
 Fixed size: The quick brown fox jumped over the lazy dog
 After Sort,
 Standard  : brown dog fox jumped lazy over quick the The
 Fixed size: brown dog fox jumped lazy over quick the The
 After Reverse,
 Standard  : The the quick over lazy jumped fox dog brown
 Fixed size: The the quick over lazy jumped fox dog brown
 After adding to the standard ArrayList,
 Standard  : The the quick over lazy jumped fox dog brown AddMe
 Fixed size: The the quick over lazy jumped fox dog brown AddMe

 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Add(Object obj)
    at SamplesArrayList.main(String[] args)
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
    at SamplesArrayList.main(String[] args)

 */
import System;
import System.Collections;


// Creates and initializes a new ArrayList.
var myAL : ArrayList = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );

// Create a fixed-size wrapper around the ArrayList.
var myFixedSizeAL : ArrayList = 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 ( myException : Exception  )  {
  Console.WriteLine("Exception: " + myException.ToString());
}
try  {
  myFixedSizeAL.Insert( 3, "InsertMe" );
} catch ( myException : Exception )  {
  Console.WriteLine("Exception: " + myException.ToString());
}

 
function PrintValues( myList : IEnumerable, mySeparator : char )  {
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
   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 jumped over the lazy dog
 Fixed size: The quick brown fox jumped over the lazy dog
 After Sort,
 Standard  : brown dog fox jumped lazy over quick the The
 Fixed size: brown dog fox jumped lazy over quick the The
 After Reverse,
 Standard  : The the quick over lazy jumped fox dog brown
 Fixed size: The the quick over lazy jumped fox dog brown
 After adding to the standard ArrayList,
 Standard  : The the quick over lazy jumped fox dog brown AddMe
 Fixed size: The the quick over lazy jumped fox dog brown AddMe
 
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Add(Object obj)
    at JScript 0.Global Code()
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
    at JScript 0.Global Code()
 */ 

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

ArrayList 클래스
ArrayList 멤버
System.Collections 네임스페이스