【问题标题】:How can I insert a byte into a nested byte array at an specific position?如何将一个字节插入到特定位置的嵌套字节数组中?
【发布时间】: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


    【解决方案1】:

    您也可以通过索引器访问嵌套列表集合。然后使用Insert方法在你需要的位置插入数据。

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

    【讨论】:

    • 如果ByteToInsert是一个字节数组,我可以用for循环遍历吗?
    • @Jacobus21 如果ByteToInsertbyte[],那么它的名字就很糟糕!但是:InsertRange
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2012-10-18
    • 2018-03-02
    • 2015-06-09
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多