简体   繁体   English

如何在特定位置的嵌套字节数组中插入一个字节?

[英]How can I insert a byte into a nested byte array at an specific position?

I have multiple byte lists ( lines ) inside another list (nested lists). 我在另一个列表(嵌套列表)中有多个字节列表( )。 How can I insert a specific byte at an specific index at an specific line? 如何在特定行的特定索引处插入特定字节?

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
...

After executing the insert code, NestedLists should consist of two lines: 执行插入代码后,NestedLists应该由两行组成:

{ 2, 2, 5, 25 }
{ 3, 7, 8, 1, 35 }

How can I accomplish this? 我该怎么做?

Solution

thanks to Hamlet Hakobyan and Marc Gravell♦: 感谢Hamlet Hakobyan和Marc Gravell♦:

If a single byte is to be inserted: 如果要插入一个字节:

NestedList[LineNumber - 1].Insert(IndexInSubList, ByteToInsert);

If a byte array is to be inserted: 如果要插入字节数组:

NestedList[LineNumber - 1].InsertRange(IndexInSubList, BytesToInsert);

You can access to the nested list collection also by indexer. 您也可以通过索引器访问嵌套列表集合。 Then use Insert method to insert data at the position you need. 然后使用Insert方法将数据插入所需的位置。

NestedList[LineNumber-1].Insert(IndexInSubList, ByteToInsert);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM