本人博客园首个随笔如何处理数值系列

声明:本人第一次写..如有不足..或有错误...敬请提出.......希望可以通过大家的帮助得到更多知识...

 

先来谈谈..我在一个项目中遇到的问题吧

我在一个项目中:要将

data,couter

100,1

100,1

102,1

103,2

103,4

105,2

105,3

105,2

目的:要将相同关键字的data中的couter累加到一起..

100,2

102,1

103,6

105,7

由于在C#中有命名空间中

using System.Collections;
using System.Collections.Specialized;

有这样的一些

NameValueCollection

集合类;

我相信这些集合可以帮助我处理这个问题;

官方给NameValueCollection定义为特殊集合一类,在System.Collections.Specialized下。
System.Collections.Specialized下还有HybridDicionary类,建议少于10个元素用HybridDicionary,当元素增加会自动转为HashTable。
System.Collections.Specialized下还有HybridDicionary类,字符串集合。
System.Collections.Specialized下还有其他类大家可以各取所需!
言归正转主要说NameValueCollection,HashTable 和 NameValueCollection很类似但是他们还是有区别的,HashTable 的KEY是唯一性,而NameValueCollection则不唯一!

在仔细参考啦不少的资料后,我便开始下手:

static void Main(string[] args)
        {

///初始化测试数据;
            ArrayList data=new ArrayList();
            ArrayList couter=new ArrayList();
            data.Add(100);
             data.Add(100);
            data.Add(102);
        
           
            couter.Add(1);
             couter.Add(1);
            couter.Add(1);


            MakeDataCouter(data, couter);//


        }

 /// <summary>
        /// 用于...得到一组没有重复关键字的集合,而且这个集合中..重复的数值.的个数也计算出来..实现啦...//
        /// </summary>
        /// <param name="data"></param>
        /// <param name="couter"></param>
        /// <returns></returns>
        public static NameValueCollection MakeDataCouter(ArrayList data, ArrayList couter)
        {
       
            //强度;
            NameValueCollection myCol = new NameValueCollection();
            for (int i = 0; i < data.Count; i++)
            {
                myCol.Add(data[i].ToString(), couter[i].ToString());

            }

            ArrayList c = new ArrayList();
            ArrayList d= new ArrayList();
          for (int n = 0; n < myCol.Count; n++)
            {
               c.Add(myCol.Get(n).ToString());//
               d.Add(myCol.GetKey(n).ToString());
                //myCol.GetKey(data[n].ToString());
            }
          ArrayList NorepeatCouter = new ArrayList();//没有重复的元素ID个数;也就是每个元素ID的个数;

          for (int k = 0; k < d.Count; k++)
          {

              string[] cc = c[k].ToString().Split(',');//第一个元素的个数和;
             NorepeatCouter.Add(Num(cc));
              //计算元素ID个数;
          }

          NameValueCollection EidAndcouter = new NameValueCollection();
         for (int q = 0; q< d.Count; q++)
          {

              EidAndcouter.Add(d[q].ToString(), NorepeatCouter[q].ToString());
          }


         return EidAndcouter;
        }

  /// <summary>
        /// 用来对数组内部累加....例如1,1累加2;
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static int Num(string[] cc)
        {
            int s=0;
            for (int k = 0; k <cc.Length; k++)
            {

               //计算元素ID个数;
               
           
                    s=s+Convert.ToInt32(cc[k].ToString());
                    
            
            }
     
            return s;
        }

代码不难......就不多加解释...

很晚啦,睡觉去....过几天就要大四啦,...先写着吧.....如果大家有什么想法可以留言....

作者:johnny 出处:http://www.cnblogs.com/sunjunlin 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/sunjunlin/p/NameValueCollection.html