我有
var previous = new BitArray(new bool[]{true}); var current = new BitArray(new bool[]{false});
我想连接它们.我已经尝试过了:
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;
但它看起来不是最好的方法.
不幸的是,你的方法看起来可能和它一样好 - 如果BitArray实现IEnumerable
如果我是你,我会把它包装成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); }
Cast
在bitarray'变为' 后,可以使用LINQ执行此操作IEnumerable
:
var previous = new BitArray(new bool[] { true }); var current = new BitArray(new bool[] { false }); BitArray newBitArray = new BitArray(previous.Cast().Concat(current.Cast ()).ToArray());
我不认为这种LINQ方法会很快.