在 C# 中獲取或設定 BitArray 中的元素數


獲取或設定 BitArray 中的元素數的程式碼如下 -

示例

 即時演示

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      BitArray arr1 = new BitArray(2);
      BitArray arr2 = new BitArray(2);
      arr1[0] = false;
      arr1[1] = true;
      Console.WriteLine("BitArray1 length = "+arr1.Length);
      Console.WriteLine("Elements in BitArray1...");
      foreach (bool res in arr1) {
         Console.WriteLine(res);
      }
      arr2[0] = false;
      arr2[1] = true;
      Console.WriteLine("
BitArray2 length = "+arr2.Length);       Console.WriteLine("Elements in BitArray2...");       foreach (bool res in arr2) {          Console.WriteLine(res);       }       Console.WriteLine("
Is BitArray1 equal to BitArray2? = "+arr2.Equals(arr1));    } }

輸出

這將產生以下輸出 -

BitArray1 length = 2
Elements in BitArray1...
False
True
BitArray2 length = 2
Elements in BitArray2...
False
True
Is BitArray1 equal to BitArray2? = False

示例

我們再看另一個示例 -

 即時演示

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      BitArray arr1 = new BitArray(2);
      BitArray arr2 = new BitArray(1);
      arr1[0] = false;
      arr1[1] = true;
      Console.WriteLine("BitArray1 length = "+arr1.Length);
      Console.WriteLine("Elements in BitArray1...");
      foreach (bool res in arr1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("
BitArray2 length = "+arr2.Length);       Console.WriteLine("Elements in BitArray2...");       foreach (bool res in arr2) {          Console.WriteLine(res);       }    } }

輸出

這將產生以下輸出 -

BitArray1 length = 2
Elements in BitArray1...
False
True
BitArray2 length = 1
Elements in BitArray2...
False

更新日期: 2019-12-10

69 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.