c#字典中删除值为某浮点数

2023-08-24 13:38:34 浏览数 (1)

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, float> m_dic = new Dictionary<int, float>();
            m_dic.Add(1,1.023f);
            m_dic.Add(2, 1.023f);
            m_dic.Add(3, 1.0234f);
            DicRemove(m_dic,1.023f);
        }

        public static void DicRemove(Dictionary<int, float> dic, float iValue)
        {
            List<int> list = new List<int>();
            foreach (var it in dic)
            {
                if ((iValue - 0.000001f) < it.Value && it.Value < (iValue   0.000001f))
                {
                    list.Add(it.Key);
                }
            }
            for (int i = list.Count - 1; i >= 0; i--)
            {
                dic.Remove(list[i]);
            }
            foreach (var it in dic)
            {
                Console.WriteLine(it.Value   " ");
            }
            Console.ReadLine();
        }
    }
}

0 人点赞