zoukankan      html  css  js  c++  java
  • c#删除 list中的元素和怎么去除空元素

    for (int i = list.Count - 1; i >= 0; i--)
    {
      if (list[i].NO == item.NO)
      {
      list.RemoveAt(i);
      }
    }
     public void RemoveItemFromList(ref List <A> list, A item)
            {
                List <A> tempList = new List <A>();
                foreach (A a in list)
                {
                    if (a.NO != item.NO && !tempList.Contains(a))
                        tempList.Add(a);
                }
                list = tempList;
            }

    如果支除空元素,可以使用Split参数StringSplitOptions.RemoveEmptyEntries去实现

    用法:

     string test = "程$晓$";
      使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries);
      输出结果:数组长度为2 temp[0]="" temp[1]="";
     
    
      使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.None);或string[] temp = test.Split('$');
    
    
      输出结果:数组长度为3 temp[0]="" temp[1]="" temp[2]="";
      

    string[] inputpids = productIds.IndexOf(',') > 0 ? productIds.Split(',').Distinct().ToArray() : new string[] { productIds };
    int[] outputpids = Array.ConvertAll<string, int>(inputpids, delegate(string s)
    {
    var val = 0;
    int.TryParse(s, out val);
    return val;
    });
    req.ProductIds = outputpids.Where(c=>c>0).ToList();

  • 相关阅读:
    Math类操作数据
    java之静态方法与非静态方法
    使用Date和SimpleDateFormat类表示时间
    Java 中基本类型和字符串之间的转换
    Python基础
    生成对抗网络
    机器翻译
    语义角色标注
    个性化推荐
    词向量
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/5186363.html
Copyright © 2011-2022 走看看