简体   繁体   中英

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:

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

How can I accomplish this?

Solution

thanks to Hamlet Hakobyan and 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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