加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

哪个c#集合使用而不是List>?

发布时间:2020-12-15 08:19:21 所属栏目:百科 来源:网络整理
导读:我想存储数据,如 { {"apple",15 } {"pear",12.5 } {"",10 } {"",0.45 }} 数据将绘制在条形图上(字符串将是图例,double将是值) 插入顺序很重要. Perfs并不重要. 字符串可以重复或为空. (值也可能重复) 我需要获得最小值和最大值(如果可能的话,很容易)来设置比
我想存储数据,如
{
 {"apple",15   }
 {"pear",12.5  }
 {"",10       }
 {"",0.45     }
}

数据将绘制在条形图上(字符串将是图例,double将是值)
插入顺序很重要.
Perfs并不重要.
字符串可以重复或为空. (值也可能重复)

我需要获得最小值和最大值(如果可能的话,很容易)来设置比例.

我用

List<KeyValuePair<string,double>> data = new List<KeyValuePair<string,double>>();
data.Add(new KeyValuePair<string,double>("",i));

相当无聊和难以理解.
有更清洁的方法吗?

StringDoubleCollection data = new StringDoubleCollection();
data.add("apple",15);
data.add("",10);

double max = data.values.Max(); 
double min = data.values.Min();

如果不是如何获得List< KeyValuePair< string,double>>的最大值没有太多的麻烦

NameValueCollection看起来很不错但是它的< string,string>我需要一个< string,double>

谢谢

解决方法

要确定您真正想要的数据结构,请查看您的使用模式.

>插入订单事宜.
>您不能通过密钥访问您的项目.
>你想要最小和最大.

堆提供最小值或最大值,但不保留顺序.基于哈希的字典也不保留顺序. List实际上是您数据结构的不错选择.它是可用的,并提供出色的支持.

您可以通过为数据结构和条形数据定义类来美化代码.您可以为集合添加最小/最大功能.注意:我没有使用Linq Min / Max函数,因为它们返回最小值,而不是最小元素.

public class BarGraphData {
    public string Legend { get; set; }
    public double Value { get; set; }
}

public class BarGraphDataCollection : List<BarGraphData> {
    // add necessary constructors,if any

    public BarGraphData Min() {
        BarGraphData min = null;
        // finds the minmum item
        // prefers the item with the lowest index
        foreach (BarGraphData item in this) {
            if ( min == null )
                min = item;
            else if ( item.Value < min.Value )
                min = item;
        }
        if ( min == null )
            throw new InvalidOperationException("The list is empty.");
        return min;
    }

    public BarGraphData Max() {
        // similar implementation as Min
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读