using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ropin.Inspection.Common.Helper { public class ListHelper { public static void RemoveNull(List list) { for (int i = list.Count - 1; i >= 0; i--) if (list[i] == null) list.RemoveAt(i); // O(n) } public static void RemoveNullTwo(List list) { // 找出第一个空元素 O(n) int count = list.Count; for (int i = 0; i < count; i++) if (list[i] == null) { // 记录当前位置 int newCount = i++; // 对每个非空元素,复制至当前位置 O(n) for (; i < count; i++) if (list[i] != null) list[newCount++] = list[i]; // 移除多余的元素 O(n) list.RemoveRange(newCount, count - newCount); break; } } public static List RemoveNullThree(List oldList) { List listTemp = new List(); for (int i = oldList.Count - 1; i >= 0; i--) if (oldList[i] != null) listTemp.Add(oldList[i]); return listTemp; } } }