c#_集合类[ ArrayList]和键值对集合[Hashtable]

ArrayList[集合类]:可以添加、移除、遍历;可变长度,用法和数组类似
数组特点:1.类型统一、2.长度固定
属性:count(得到集合中的实际存放数据的个数)
方法:Add()添加   remove()移除  RemoveAt()从零开始索引,然后删除对象。  ToArry()将集合转成数组
引用快捷键:ctrl+Alt+F10


ArrayList[集合类]的建立、添加:
ArrayList arrayList=new ArrayList();
Console.WriteLine(arrayList.Count);
arrayList.Add(10);
arrayList.Add("HELLO WORLD");
arrayList.Add(3.45);
Console.WriteLine(arrayList.Count);//元素实际个数集合中
Console.WriteLine(arrayList.Capacity);
Console.ReadKey();


ArrayList[集合类]移除:
arrayList.Remove(3.45);


ArrayList[集合类]遍历:
for(int i=0;i<9;i++)
{
arrayList.RemoveAt(0);
}
arrayList.TrimToSize();//缩减数组容量


Hashtable[健值对的集合]
Hashtable hashtable=new Hashtable();//Hashtable解析
//定义Person实例
Person YQQ=new Person(){Name="杨巧巧"};
hash.Add(YQQ.Name,YQQ);


Person Yy=new Person(){Name="尤越"};
hash.Add(Yy.Name,Yy);


//取值
Console.WriteLine( ((Person) hash["杨巧巧"]).Name);
方法:Add(object key,object value);
hash["key"]="修改";
Remove("key");


#region 
程序运行时不运行
#endregion


遍历查找
for (int i = 0; i < arrInt.Length; i++)
{
 if (arrInt[i] == number)
  {
    b = true;
  }


}
if (b)
{
 Console.WriteLine("存在!");
 }
 else
{
 Console.WriteLine("不存在!");
 }
//健的地址是算出来的,不是遍历得到得,所以才效率高
int[] arrInt={25,125,225,325,425,525,625};
int number=325;
bool b=false;
int index=number/100;
if(index<arrInt.Length)
{
Console.WriteLine("可能存在!");
Console.WriteLine(arrInt[index]);
}
else
{
Console.WriteLine("not exist");
}
Console.ReadKey();


if(hash.ContainsKey("张兰"))
{
Console.WriteLine("包含");
}
else
{
Console.WriteLine("不包含");
}


遍历Hsahtable
Hsahtable table=new Hsahtable();
table.Add("qq","YangQiaoQiao");
table.Add("lxf","LiuXiaoFei");
table.Add("zl","ZhangLan");
foreach(DictionaryEntry item in table)
{//DictionaryEntry是个结构体
Console.WriteLine(item.key+"  "+item.Value);
}
foreach(var item in table.Keys)
{
Console.WriteLine(itme);
}
foreach(var item in table.Value)
{
Console.WriteLine(itme);
}
//var 当变量没有赋值时、变量可能是任意一种类型;一旦给变量赋了值,变量就成了值得类型。


作业:
1.把两个(ArrayList)集合{"a","b","c","d"}和{"c","f","d","h"},  合并这两个集合并除去重复项。合并为一个新的arrayList集合对象
ArrayList arrayList1=new ArrayList(){"a","b","c","d"};
ArrayList arrayList2=new ArrayList(){"c","f","d","h"};
ArrayList arrayList3=new ArrayList();


arrayList3.AddRange(arryList1);
for(int i=0;i<arrayList2.count;i++)
{
if(!arrayList3.Contains(arrayList2[i]))
{
arrayList3.AddRange(arryList2[i]);
}
}
for(int i=0;i<arrayList3.Count;i++)
{
Console.WriteLine(arrayList3[i]);
}
2.随机生成10个1-100之间的数,放到ArrayList中,要求这10个数不能重复,并且都是偶数。添加十次,但是会循环很多次。
ArrayList arrayRandom = new ArrayList();          
 Random random = new Random();
 while (arrayRandom.Count < 10)
{
 int ra= random.Next(1, 101);
 if (ra % 2 == 0)

if( !arrayRandom.Contains(ra))
{
 arrayRandom.Add(ra);
                      
 }
                
  }
 }
 Console.ReadKey();





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值