📜  C#|如何创建BitArray的浅表副本

📅  最后修改于: 2021-05-30 01:12:04             🧑  作者: Mango

BitArray.Clone方法用于创建BitArray的浅表副本。此方法仅复制集合的元素,无论它们是引用类型还是值类型,但不复制引用所引用的对象。

句法:

public object Clone ();

示例1:在这里,我们创建一个字节类型的数组。然后声明一个类型为BitArray的对象B1 ,并使用之前创建的字节数组对其进行初始化。声明另一个BitArray类型的对象B2 ,该对象将用于存储B1的副本。指令B1.Clone返回类型为System.Collections的B1的浅表副本。因此,在将其存储到B2对象之前,先将其显式转换为BitArray。最后,显示克隆的BitArray。

// C# program to demonstrate the 
// BitArray.Clone Method
using System;
using System.Collections;
  
class GFG {
      
    static void Main(string[] args)
    {
        // Declaring a byte array
        byte[] ByteArray = new byte[] {1, 3};
  
        // Declaring a BitArray object
        // Initializing to the byte array
        BitArray B1 = new BitArray(ByteArray);
          
        // Declaring a new BitArray object
        BitArray B2 = new BitArray(4);
  
        // Using the BitArray.Clone method
        B2 = (BitArray)B1.Clone();
  
        // Displaying the length of the BitArray
        Console.WriteLine("Length of B2: {0}", B2.Length);
          
        Console.WriteLine("\nB2 Contains:");
  
        // To display the values
        // of BitArray object B2
        foreach(Object item in B2)
        {
            Console.WriteLine(item);
        }
  
    } 
}
输出:
Length of B2: 16

B2 Contains:
True
False
False
False
False
False
False
False
True
True
False
False
False
False
False
False

示例2:浅表副本仅复制BitArray的内容,而不复制对象引用,对B2所做的任何更改将仅更新B2中的那些更改,而不更新B1中的那些更改。

// C#Program to show changes in clone
// don't affect the original BitArray
using System;
using System.Collections;
  
class GFG {
      
    static void Main(string[] args)
    {
        // Declaring a bool array
        bool[] BooleanArray = new bool[] {true, 
                          false, true, false };
  
        // Declaring an object B1 of BitArray
        // Initialising with the bool array
        BitArray B1 = new BitArray(BooleanArray);
          
        int i;
  
        // Declaring object B2 of BitArray
        BitArray B2 = new BitArray(4);
  
        // Using the BitArray.Clone method
        B2 = (BitArray)B1.Clone();
  
        i = 4;
  
        // Displaying elements of B2
        Console.WriteLine("B2 Before Making any changes:");
          
        foreach(Object item in B2)
        {
            if (i <= 0) 
            {
                Console.WriteLine();
                i = 6;
            }
            i--;
            Console.Write("{0, 4} ", item);
        }
  
        // Updating elements of B2
        // at index 0 and 1
        B2[0] = false;
        B2[1] = true;
  
        i = 4;
  
        // Displaying elements 
        // of B2 after updating
        Console.WriteLine("\n\nB2 After changes:");
          
        foreach(Object item in B2)
        {
            if (i <= 0) 
            {
                Console.WriteLine();
                i = 6;
            }
            i--;
            Console.Write("{0, 4} ", item);
        }
  
        // Displaying elements of B1
        Console.WriteLine("\n\nB1 After Changes:");
          
        i = 4;
          
        foreach(Object item in B1)
        {
            if (i <= 0) 
            {
                Console.WriteLine();
                i = 6;
            }
            i--;
            Console.Write("{0, 4} ", item);
        }
  
    } 
}
输出:
B2 Before Making any changes:
True False True False 

B2 After changes:
False True True False 

B1 After Changes:
True False True False

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.bitarray.clone?view=netframework-4.7.2