BitArray BitArray BitArray BitArray Class

定义

管理位值的压缩数组,这些值以布尔值的形式表示,其中 true 表示此位为开 (1),false 表示此位为关 (0)。Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).

public ref class BitArray sealed : ICloneable, System::Collections::ICollection
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class BitArray : ICloneable, System.Collections.ICollection
type BitArray = class
    interface ICollection
    interface ICloneable
    interface IEnumerable
Public NotInheritable Class BitArray
Implements ICloneable, ICollection
继承
BitArrayBitArrayBitArrayBitArray
属性
实现

示例

下面的代码示例演示如何创建和初始化BitArray , 以及如何打印出其值。The following code example shows how to create and initialize a BitArray and how to print out its values.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, int myWidth );
int main()
{
   
   // Creates and initializes several BitArrays.
   BitArray^ myBA1 = gcnew BitArray( 5 );
   BitArray^ myBA2 = gcnew BitArray( 5,false );
   array<Byte>^myBytes = {1,2,3,4,5};
   BitArray^ myBA3 = gcnew BitArray( myBytes );
   array<Boolean>^myBools = {true,false,true,true,false};
   BitArray^ myBA4 = gcnew BitArray( myBools );
   array<Int32>^myInts = {6,7,8,9,10};
   BitArray^ myBA5 = gcnew BitArray( myInts );
   
   // Displays the properties and values of the BitArrays.
   Console::WriteLine( "myBA1" );
   Console::WriteLine( "   Count:    {0}", myBA1->Count );
   Console::WriteLine( "   Length:   {0}", myBA1->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA1, 8 );
   Console::WriteLine( "myBA2" );
   Console::WriteLine( "   Count:    {0}", myBA2->Count );
   Console::WriteLine( "   Length:   {0}", myBA2->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA2, 8 );
   Console::WriteLine( "myBA3" );
   Console::WriteLine( "   Count:    {0}", myBA3->Count );
   Console::WriteLine( "   Length:   {0}", myBA3->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA3, 8 );
   Console::WriteLine( "myBA4" );
   Console::WriteLine( "   Count:    {0}", myBA4->Count );
   Console::WriteLine( "   Length:   {0}", myBA4->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA4, 8 );
   Console::WriteLine( "myBA5" );
   Console::WriteLine( "   Count:    {0}", myBA5->Count );
   Console::WriteLine( "   Length:   {0}", myBA5->Length );
   Console::WriteLine( "   Values:" );
   PrintValues( myBA5, 8 );
}

void PrintValues( IEnumerable^ myList, int myWidth )
{
   int i = myWidth;
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      if ( i <= 0 )
      {
         i = myWidth;
         Console::WriteLine();
      }

      i--;
      Console::Write( "{0,8}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 myBA1
    Count:    5
    Length:   5
    Values:
    False   False   False   False   False
 myBA2
    Count:    5
    Length:   5
    Values:
    False   False   False   False   False
 myBA3
    Count:    40
    Length:   40
    Values:
     True   False   False   False   False   False   False   False
    False    True   False   False   False   False   False   False
     True    True   False   False   False   False   False   False
    False   False    True   False   False   False   False   False
     True   False    True   False   False   False   False   False
 myBA4
    Count:    5
    Length:   5
    Values:
     True   False    True    True   False
 myBA5
    Count:    160
    Length:   160
    Values:
    False    True    True   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
     True    True    True   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False    True   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
     True   False   False    True   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False    True   False    True   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
    False   False   False   False   False   False   False   False
 */
using System;
using System.Collections;
public class SamplesBitArray  {

   public static void Main()  {

      // Creates and initializes several BitArrays.
      BitArray myBA1 = new BitArray( 5 );

      BitArray myBA2 = new BitArray( 5, false );

      byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 };
      BitArray myBA3 = new BitArray( myBytes );

      bool[] myBools = new bool[5] { true, false, true, true, false };
      BitArray myBA4 = new BitArray( myBools );

      int[]  myInts  = new int[5] { 6, 7, 8, 9, 10 };
      BitArray myBA5 = new BitArray( myInts );

      // Displays the properties and values of the BitArrays.
      Console.WriteLine( "myBA1" );
      Console.WriteLine( "   Count:    {0}", myBA1.Count );
      Console.WriteLine( "   Length:   {0}", myBA1.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA1, 8 );

      Console.WriteLine( "myBA2" );
      Console.WriteLine( "   Count:    {0}", myBA2.Count );
      Console.WriteLine( "   Length:   {0}", myBA2.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA2, 8 );

      Console.WriteLine( "myBA3" );
      Console.WriteLine( "   Count:    {0}", myBA3.Count );
      Console.WriteLine( "   Length:   {0}", myBA3.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA3, 8 );

      Console.WriteLine( "myBA4" );
      Console.WriteLine( "   Count:    {0}", myBA4.Count );
      Console.WriteLine( "   Length:   {0}", myBA4.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA4, 8 );

      Console.WriteLine( "myBA5" );
      Console.WriteLine( "   Count:    {0}", myBA5.Count );
      Console.WriteLine( "   Length:   {0}", myBA5.Length );
      Console.WriteLine( "   Values:" );
      PrintValues( myBA5, 8 );
   }

   public static void PrintValues( IEnumerable myList, int myWidth )  {
      int i = myWidth;
      foreach ( Object obj in myList ) {
         if ( i <= 0 )  {
            i = myWidth;
            Console.WriteLine();
         }
         i--;
         Console.Write( "{0,8}", obj );
      }
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

myBA1
   Count:    5
   Length:   5
   Values:
   False   False   False   False   False
myBA2
   Count:    5
   Length:   5
   Values:
   False   False   False   False   False
myBA3
   Count:    40
   Length:   40
   Values:
    True   False   False   False   False   False   False   False
   False    True   False   False   False   False   False   False
    True    True   False   False   False   False   False   False
   False   False    True   False   False   False   False   False
    True   False    True   False   False   False   False   False
myBA4
   Count:    5
   Length:   5
   Values:
    True   False    True    True   False
myBA5
   Count:    160
   Length:   160
   Values:
   False    True    True   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
    True    True    True   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
    True   False   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False    True   False    True   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
   False   False   False   False   False   False   False   False
*/ 

Imports System.Collections

Public Class SamplesBitArray

    Public Shared Sub Main()

        ' Creates and initializes several BitArrays.
        Dim myBA1 As New BitArray(5)

        Dim myBA2 As New BitArray(5, False)

        Dim myBytes() As Byte = {1, 2, 3, 4, 5}
        Dim myBA3 As New BitArray(myBytes)

        Dim myBools() As Boolean = {True, False, True, True, False}
        Dim myBA4 As New BitArray(myBools)

        Dim myInts() As Integer = {6, 7, 8, 9, 10}
        Dim myBA5 As New BitArray(myInts)

        ' Displays the properties and values of the BitArrays.
        Console.WriteLine("myBA1")
        Console.WriteLine("   Count:    {0}", myBA1.Count)
        Console.WriteLine("   Length:   {0}", myBA1.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA1, 8)

        Console.WriteLine("myBA2")
        Console.WriteLine("   Count:    {0}", myBA2.Count)
        Console.WriteLine("   Length:   {0}", myBA2.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA2, 8)

        Console.WriteLine("myBA3")
        Console.WriteLine("   Count:    {0}", myBA3.Count)
        Console.WriteLine("   Length:   {0}", myBA3.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA3, 8)

        Console.WriteLine("myBA4")
        Console.WriteLine("   Count:    {0}", myBA4.Count)
        Console.WriteLine("   Length:   {0}", myBA4.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA4, 8)

        Console.WriteLine("myBA5")
        Console.WriteLine("   Count:    {0}", myBA5.Count)
        Console.WriteLine("   Length:   {0}", myBA5.Length)
        Console.WriteLine("   Values:")
        PrintValues(myBA5, 8)

    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable, myWidth As Integer)
        Dim i As Integer = myWidth
        Dim obj As [Object]
        For Each obj In  myList
            If i <= 0 Then
                i = myWidth
                Console.WriteLine()
            End If
            i -= 1
            Console.Write("{0,8}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class 'SamplesBitArray 


' This code produces the following output.
' 
' myBA1
'    Count:    5
'    Length:   5
'    Values:
'    False   False   False   False   False
' myBA2
'    Count:    5
'    Length:   5
'    Values:
'    False   False   False   False   False
' myBA3
'    Count:    40
'    Length:   40
'    Values:
'     True   False   False   False   False   False   False   False
'    False    True   False   False   False   False   False   False
'     True    True   False   False   False   False   False   False
'    False   False    True   False   False   False   False   False
'     True   False    True   False   False   False   False   False
' myBA4
'    Count:    5
'    Length:   5
'    Values:
'     True   False    True    True   False
' myBA5
'    Count:    160
'    Length:   160
'    Values:
'    False    True    True   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'     True    True    True   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False    True   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'     True   False   False    True   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False    True   False    True   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False
'    False   False   False   False   False   False   False   False

注解

BitArray类是一个集合类, 其中的容量始终与计数相同。The BitArray class is a collection class in which the capacity is always the same as the count. 通过增加属性将元素添加到Length , 通过减少属性删除元素。 Length BitArrayElements are added to a BitArray by increasing the Length property; elements are deleted by decreasing the Length property. 的大小BitArray由客户端控制; 结束BitArray后的索引将引发ArgumentExceptionOr And Not Xor类提供在其他集合中找不到的方法, 包括允许使用筛选器一次性修改多个元素的方法, 例如、、、和。 BitArray SetAll.The size of a BitArray is controlled by the client; indexing past the end of the BitArray throws an ArgumentException.The BitArray class provides methods that are not found in other collections, including those that allow multiple elements to be modified at once using a filter, such as And, Or, Xor , Not, and SetAll.

类是一个结构, 它提供与相同的BitArray功能, 但性能更高。 BitVector32The BitVector32 class is a structure that provides the same functionality as BitArray, but with faster performance. BitVector32速度更快, 因为它是值类型, 因此在堆栈上分配, BitArray而是引用类型, 因此在堆上分配。BitVector32 is faster because it is a value type and therefore allocated on the stack, whereas BitArray is a reference type and, therefore, allocated on the heap.

System.Collections.Specialized.BitVector32可以存储精确到32位, BitArray而可以存储可变位数的位数。System.Collections.Specialized.BitVector32 can store exactly 32 bits, whereas BitArray can store a variable number of bits. BitVector32存储位标志和小整数, 从而使其成为不向用户公开的数据的理想选择。BitVector32 stores both bit flags and small integers, thereby making it ideal for data that is not exposed to the user. 但是, 如果所需位标志的数目未知、可变或大于 32, 请BitArray改用。However, if the number of required bit flags is unknown, is variable, or is greater than 32, use BitArray instead.

BitArray位于System.Collections命名空间中;BitVector32位于System.Collections.Specialized命名空间中。BitArray is in the System.Collections namespace; BitVector32 is in the System.Collections.Specialized namespace.

可以使用整数索引访问此集合中的元素。Elements in this collection can be accessed using an integer index. 此集合中的索引从零开始。Indexes in this collection are zero-based.

构造函数

BitArray(BitArray) BitArray(BitArray) BitArray(BitArray) BitArray(BitArray)

初始化包含从指定 BitArray 复制的位值的 BitArray 类的新实例。Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray.

BitArray(Boolean[]) BitArray(Boolean[]) BitArray(Boolean[]) BitArray(Boolean[])

初始化 BitArray 类的新实例,该实例包含从布尔值指定数组复制的位值。Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans.

BitArray(Byte[]) BitArray(Byte[]) BitArray(Byte[]) BitArray(Byte[])

初始化 BitArray 类的新实例,该实例包含从指定的字节数组复制的位值。Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes.

BitArray(Int32) BitArray(Int32) BitArray(Int32) BitArray(Int32)

初始化 BitArray 类的新实例,该实例可拥有指定数目的位值,位值最初设置为 falseInitializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to false.

BitArray(Int32, Boolean) BitArray(Int32, Boolean) BitArray(Int32, Boolean) BitArray(Int32, Boolean)

初始化 BitArray 类的新实例,此实例可以容纳指定数量的位值,并且将其初始值设置为指定值。Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to the specified value.

BitArray(Int32[]) BitArray(Int32[]) BitArray(Int32[]) BitArray(Int32[])

初始化 BitArray 类的新实例,该类包含从指定的 32 位整数数组复制的位值。Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers.

属性

Count Count Count Count

获取 BitArray 中包含的元素数。Gets the number of elements contained in the BitArray.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

获取一个值,该值指示 BitArray 是否为只读。Gets a value indicating whether the BitArray is read-only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

获取一个值,该值指示是否同步对 BitArray 的访问(线程安全)。Gets a value indicating whether access to the BitArray is synchronized (thread safe).

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取或设置 BitArray 中特定位置的位的值。Gets or sets the value of the bit at a specific position in the BitArray.

Length Length Length Length

获取或设置 BitArray 中元素的数目。Gets or sets the number of elements in the BitArray.

SyncRoot SyncRoot SyncRoot SyncRoot

获取可用于同步对 BitArray 的访问的对象。Gets an object that can be used to synchronize access to the BitArray.

方法

And(BitArray) And(BitArray) And(BitArray) And(BitArray)

在当前 BitArray 对象中的元素和指定数组中的相应元素之间执行按位“与”运算。Performs the bitwise AND operation between the elements of the current BitArray object and the corresponding elements in the specified array. 将修改当前 BitArray 对象,以存储按位“与”运算的结果。The current BitArray object will be modified to store the result of the bitwise AND operation.

Clone() Clone() Clone() Clone()

创建 BitArray 的浅表副本。Creates a shallow copy of the BitArray.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 BitArray 复制到兼容的一维 ArrayCopies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Get(Int32) Get(Int32) Get(Int32) Get(Int32)

获取 BitArray 中特定位置处的位的值。Gets the value of the bit at a specific position in the BitArray.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回循环访问 BitArray的枚举数。Returns an enumerator that iterates through the BitArray.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
LeftShift(Int32) LeftShift(Int32) LeftShift(Int32) LeftShift(Int32)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Not() Not() Not() Not()

反转当前 BitArray 中的所有位值,以便将设置为 true 的元素更改为 false;将设置为 false 的元素更改为 trueInverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true.

Or(BitArray) Or(BitArray) Or(BitArray) Or(BitArray)

在当前 BitArray 对象中的元素和指定数组中的相应元素间执行按位“或”运算。Performs the bitwise OR operation between the elements of the current BitArray object and the corresponding elements in the specified array. 将修改当前 BitArray 对象,以存储按位“或”运算的结果。The current BitArray object will be modified to store the result of the bitwise OR operation.

RightShift(Int32) RightShift(Int32) RightShift(Int32) RightShift(Int32)
Set(Int32, Boolean) Set(Int32, Boolean) Set(Int32, Boolean) Set(Int32, Boolean)

BitArray 中特定位置处的位设置为指定值。Sets the bit at a specific position in the BitArray to the specified value.

SetAll(Boolean) SetAll(Boolean) SetAll(Boolean) SetAll(Boolean)

BitArray 中的所有位设置为指定值。Sets all bits in the BitArray to the specified value.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
Xor(BitArray) Xor(BitArray) Xor(BitArray) Xor(BitArray)

针对指定数组中的相应元素,在当前 BitArray 对象中的元素间执行按位“异或”运算。Performs the bitwise exclusive OR operation between the elements of the current BitArray object against the corresponding elements in the specified array. 将修改当前 BitArray 对象,以存储按位“异或”运算的结果。The current BitArray object will be modified to store the result of the bitwise exclusive OR operation.

显式界面实现

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

从指定的 BitArray 索引位置开始,将 Array 的元素复制到 Array 中。Copies the elements of the BitArray to an Array, starting at the specified Array index.

ICollection.Count ICollection.Count ICollection.Count ICollection.Count

获取 BitArray 中的元素数。Gets the number of elements in the BitArray.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个值,该值指示对 BitArray 的访问是否同步(线程安全)。Gets a value that indicates whether access to the BitArray is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取可用于同步对 BitArray 的访问的对象。Gets an object that can be used to synchronize access to the BitArray.

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

线程安全性

此类型的Shared公共静态 (在 Visual Basic) 成员是线程安全的。Public static (Shared in Visual Basic) members of this type are thread safe. 但不保证所有实例成员都是线程安全的。Any instance members are not guaranteed to be thread safe.

此实现不提供的同步 (线程安全) 包装BitArray器。This implementation does not provide a synchronized (thread safe) wrapper for a BitArray.

枚举整个集合本质上不是一个线程安全的过程。Enumerating through a collection is intrinsically not a thread-safe procedure. 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

另请参阅