ArrayList.FixedSize 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回具有固定大小的清單包裝函式,其中允許修改項目,但不允許加入或移除項目。
多載
FixedSize(ArrayList) |
傳回具有固定大小的 ArrayList 包裝函式。 |
FixedSize(IList) |
傳回具有固定大小的 IList 包裝函式。 |
FixedSize(ArrayList)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
傳回具有固定大小的 ArrayList 包裝函式。
public:
static System::Collections::ArrayList ^ FixedSize(System::Collections::ArrayList ^ list);
public static System.Collections.ArrayList FixedSize (System.Collections.ArrayList list);
static member FixedSize : System.Collections.ArrayList -> System.Collections.ArrayList
Public Shared Function FixedSize (list As ArrayList) As ArrayList
參數
傳回
具有固定大小的 ArrayList 包裝函式。
例外狀況
list
為 null
。
範例
下列程式代碼範例示範如何在 周圍 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()
備註
這個包裝函式可用來防止新增與刪除原始 ArrayList。 專案仍然可以修改或取代。
具有固定大小的集合只是具有包裝函式的集合,可防止新增和移除專案;因此,如果對基礎集合進行變更,包括新增或移除元素,固定大小集合會反映這些變更。
這個方法是 O(1)
作業。
適用於
FixedSize(IList)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
傳回具有固定大小的 IList 包裝函式。
public:
static System::Collections::IList ^ FixedSize(System::Collections::IList ^ list);
public static System.Collections.IList FixedSize (System.Collections.IList list);
static member FixedSize : System.Collections.IList -> System.Collections.IList
Public Shared Function FixedSize (list As IList) As IList
參數
傳回
具有固定大小的 IList 包裝函式。
例外狀況
list
為 null
。
備註
這個包裝函式可用來防止新增與刪除原始 IList。 專案仍然可以修改或取代。
具有固定大小的集合只是具有包裝函式的集合,可防止新增和移除專案;因此,如果對基礎集合進行變更,包括新增或移除元素,固定大小集合會反映這些變更。
這個方法是 O(1)
作業。