【发布时间】:2014-03-23 20:40:16
【问题描述】:
我在另一个列表(嵌套列表)中有多个字节列表(lines)。如何在特定行的特定索引处插入特定字节?
byte ByteToInsert = 1;
int LineNumber = 2;
int IndexInSubList = 3;
// Create top-level list
List<List<byte>> NestedList = new List<List<byte>>();
// Create two nested lists
List<byte> Line1 = new List<byte> { 2, 2, 5, 25 };
List<byte> Line2 = new List<byte> { 3, 7, 8, 35 };
// Add to top-level list
NestedList.Add(Line1);
NestedList.Add(Line2);
// Insert
...
执行插入代码后,NestedLists 应该由两行组成:
{ 2, 2, 5, 25 }
{ 3, 7, 8, 1, 35 }
我怎样才能做到这一点?
解决方案
感谢 Hamlet Hakobyan 和 Marc Gravell♦:
如果要插入单个字节:
NestedList[LineNumber - 1].Insert(IndexInSubList, ByteToInsert);
如果要插入字节数组:
NestedList[LineNumber - 1].InsertRange(IndexInSubList, BytesToInsert);
【问题讨论】:
标签: c# insert byte bytearray nested-lists