C#入门及进阶|数组和集合(十一): BitArray类

本文介绍了C#中的BitArray类,包括其用于管理位值的特性,如Count属性获取元素数,Get和Set方法进行位操作,以及SetAll方法设置所有位。通过示例展示了如何创建、初始化和修改BitArray的值。
摘要由CSDN通过智能技术生成

        BitArray类用于管理位值的压缩数组,该值表示为布尔值,其中 true 表示位值为1,false表示位值为0。

        BitArray类常用的属性和方法:

属性 说明
Count 获取BitArray中包含的元素数。
方法 说明
Get获取 BitArray 中特定位置处的位的值。
Set将 BitArray 中特定位置处的位设置为指定值。
SetAll将 BitArray 中的所有位设置为指定值。

        BitArray实例: 

public static void Main()
{
    // Creates and initializes a BitArray.
    BitArray myBA = new BitArray( 5 );
    // Displays the properties and values of the BitArray.
    Console.WriteLine( "myBA values:" );
    PrintIndexAndValues( myBA );
    // Sets all the elements to true.
    myBA.SetAll( true );
    // Displays the properties and values of the BitArray.
    Console.WriteLine( "After setting all elements to true," );
    PrintIndexAndValues( myBA );
    // Sets the last index to false.
    myBA.Set( myBA.Count - 1, false );
    // Displays the properties and values of the BitArray.
    Console.WriteLine( "After setting the last element to false," );
    PrintIndexAndValues( myBA );
}
public static void PrintIndexAndValues( IEnumerable myCol )
{
    int i = 0;
    foreach ( Object obj in myCol )
    {
        Console.WriteLine( " [{0}]: {1}", i++, obj );
    }
    Console.WriteLine();
}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值