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();
3.有一个字符串是用空格分隔的一系列整数,写一个程序把其中的,奇数排列在前偶数排列在后。2 4 7 9 6 8 5---------7 9 5 2 4 6 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 练习分拣奇数偶数
{
class Program
{
static void Main(string[] args)
{
string msg = "2 7 9 3 6 5 8 4";
string[] nums = msg.Split(' ');
//7 9 3 5 2 6 8 4
//存放奇数
ArrayList listOdd = new ArrayList();
//存放偶数
ArrayList listEven = new ArrayList();
for (int i = 0; i < nums.Length; i++)
{
if (Convert.ToInt32(nums[i]) % 2 != 0)
{
listOdd.Add(nums[i]);
}
else
{
listEven.Add(nums[i]);
}
}
listOdd.AddRange(listEven);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < listOdd.Count; i++)
{
sb.Append(listOdd[i]+" ");
}
Console.WriteLine(sb);
Console.ReadKey();
}
}
}