c#一串数组,{FA,AF,00,03,00,31,31,33,FA,AF,00,02,00,34,35............}
数组长度不固定,首先找到FA,AF为特征值的两个元素,然后删除当前的两个元素以及后面的三个元素,也就是说删除FA,AF,00,03,00和FA,AF,00,02,00,剩下的元素重新生成一个数组。

C#如何删除数组中指定元素?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- threenewbee 2018-05-08 16:10关注
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { byte[] arr = { 0xFA, 0xAF, 0x00, 0x03, 0x00, 0x31, 0x31, 0x33, 0xFA, 0xAF, 0x00, 0x02, 0x00, 0x34, 0x35 }; List<int> toRemove = new List<int>() { }; bool fa = false; for (int i = 0; i < arr.Length; i++) { if (arr[i] == 0xFA) { fa = true; continue; } if (arr[i] == 0xAF && fa) { toRemove.AddRange(Enumerable.Range(i - 1, 5)); i += 3; fa = false; }; } arr = arr.Select((x, i) => new { x, i }).Where(x => !toRemove.Contains(x.i)).Select(x => x.x).ToArray(); Console.WriteLine(string.Join(" ", arr.Select(x => x.ToString("X2")))); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报