我有
var previous = new BitArray(new bool[]{true});
var current = new BitArray(new bool[]{false});
Run Code Online (Sandbox Code Playgroud)
我想连接它们.我已经尝试过了:
var next = new BitArray(previous.Count + current.Count);
var index = 0;
for(;index < previous.Count; index++)
next[index] = previous[index];
var j = 0;
for(;index < next.Count; index++, j++)
next[index] = current[j];
previous = current;
Run Code Online (Sandbox Code Playgroud)
但它看起来不是最好的方法.
不幸的是,你的方法看起来可能和它一样好 - 如果BitArray实现IEnumerable <T>(而不仅仅是IEnumerable),那么我们可以使用LINQ扩展方法使它更漂亮.
如果我是你,我会把它包装成BitArray上的扩展方法:
public static BitArray Prepend(this BitArray current, BitArray before) {
var bools = new bool[current.Count + before.Count];
before.CopyTo(bools, 0);
current.CopyTo(bools, before.Count);
return new BitArray(bools);
}
public static BitArray Append(this BitArray current, BitArray after) {
var bools = new bool[current.Count + after.Count];
current.CopyTo(bools, 0);
after.CopyTo(bools, current.Count);
return new BitArray(bools);
}
Run Code Online (Sandbox Code Playgroud)
Cast<bool>()在bitarray'变为' 后,可以使用LINQ执行此操作IEnumerable<bool>:
var previous = new BitArray(new bool[] { true });
var current = new BitArray(new bool[] { false });
BitArray newBitArray =
new BitArray(previous.Cast<bool>().Concat(current.Cast<bool>()).ToArray());
Run Code Online (Sandbox Code Playgroud)
我不认为这种LINQ方法会很快.