关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

使用集合组织相关数据

发布时间:2020-08-06 11:06:00

1)定义:

           集合:

         某些特定的对象集中在一起就是集合

            数组:

         可以存储相同数据类型的一堆数据的容器

    (2)集合

         集合分为泛型集合和非泛型集合

          泛型集合分为单列和双列集合  (List<T>和Dictionary<K,V>)

          非泛型集合分为单列和双列集合 (ArrayList和HashTable)

     (3)常用的方法和属性

         


 Add();   //添加

 Remove();  //删除

 RemobeAt(); //根据索引删除  不适用 双列集合 

 count  //集合存储元素的个数

 Contains();  //检测元素是否存在

 ContainsKey();  //

 ContainsValue();


 Capcity //集合占用空间


     注意事项:如果删除了集合中的某一个元素,那么集合的索引会自动维护

   (4)遍历方案:

     


ArrayList list=new ArrayLIst();

Student stu1=new Student();

 stu1.Name="jiejie";

 stu1.Age=15;

 list.Add(stu1);

Student stu2=new Student();

 stu2.Name="jiji";

 stu2.Age=18;

 list.Add(stu2);

  //foreachforeach(Student item in list)

{

    Console.WriteLine(item.Age+"\t"+item.Name);

}

 //forfor(int i=0;i<list.Count;i++)

{

   Console.WriteLine((Student)list[i].Name);

}


 


HashTable 遍历三种方案

   HashTable table=new HashTable();

第一种方式:遍历所有的Keysforeach(var item in table.Keys)

{

    //一个item代表一个key

   Console.WriteLine("key是{0}\tvalue是{1}",item,table[item]);

}


第二种遍历方式:遍历所有的value集合foreach(var item in table.Values)

{

       //一个item代表一个value

   Console.WriteLine("value是{0}",item);

}

//遍历整个tableforeach(DictionaryEntry item in table)

{

    Console.WriteLine("key是{0}\tvalue是{1}",item.Key,item.Value);

}



/template/Home/Zkeys/PC/Static