using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//ArrayList的简单例子
using System.Collections;//引用集合的 命名空间
namespace ArryList的简单例子
{
class Program
{
static void Main(string[] args)
{
//------------------------------------------------------------------------
/*动态维护不定长,下标(索引值)是动态维护的,它只能是一维不能多维*/
//创建ArrayList集合的对象al
ArrayList al = new ArrayList();
//向集合中添加元素
al.Add("金庸");
al.Add("黄易");
al.Add("古龙");
//通过索引输出集合中的元素
Console.WriteLine(al[1]);
//使用foreach遍历(每个元素访问一遍)集合al,输出所有元素
Console.WriteLine("集合中的所有元素:");
foreach (string s in al)/*我要找的类型,找出的结果自己命名,in,从集合中找*/
{
Console.WriteLine(s);
}
//添加元素可以动态进行
al.Add("张三");
Console.WriteLine("集合中的所有元素:");
foreach (string s in al)
{
Console.WriteLine(s);
}
//删除方法1:通过 索引 去删除集合中的元素,删除后,集合的索引会自动调整到从0开始
al.RemoveAt(0);
Console.WriteLine("删除索引为0的元素后,现在第一个元素是:" + al[0]);
//删除方法2:通过集合中的值内容去删除集合元素
al.Remove("黄易");
Console.WriteLine("删除值为“黄易”的元素后,现在第一个元素是:" + al[0]);
//清空方法:清除集合中的所有元素
al.Clear();
Console.WriteLine("清除集合中的所有元素后:");
foreach (string s in al)
{
Console.WriteLine(s);
}
//程序等待
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//命名空间
using System.Collections;
namespace ArryList与对象的操作
{
class Program
{
//------------------------------------------------------------------------
//ArrayList与对象的操作
//跟对象联系紧密,在一维数组集合中首先考虑,
//首先定义类--实例化--读出来的时候要强制类型转换--
//类
class stu
{
//字段
private string name;
private int age;
//有参数的构造方法
public stu(string name,int age)
{
this.name = name;
this.age = age;
}
//输出
public void say()
{
Console.WriteLine("姓名:{0} 年龄:{1}",name,age );
}
}
static void Main(string[] args)
{
ArrayList a = new ArrayList();
//实例化类对象并添加到集合里
stu s1 = new stu("张三",18);
stu s2 = new stu("李四",20);
stu s3 = new stu("王五",19);
stu s4 = new stu("陆六",21);
a.Add(s1);
a.Add(s2);
a.Add(s3);
a.Add(s4);
//((stu)a[0]).say();a0强制转换为stu类,通过大括号才能识别
//集合和泛型的最大不同点:集合数据不安全,
//利用循环--for
for (int i = 0; i < a.Count;i++ )
{
stu stuFor = (stu)a[i];
stuFor.say();
}
//删除方法:
a.Remove(a[0]);
a.Remove(s2);
a.RemoveAt(0);
a.Clear();
Console.WriteLine("-----------------------------------");
//利用循环--foreach
foreach (object s in a)
{
stu stuForeach = (stu)s;
stuForeach.say();
}
//程序等待
Console.ReadLine();
//通过这个例子,要理解到,ArrayList集合是可以存对象的
//并且存进去的元素全部都会转变成object的对象
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//引用集合命名空间
namespace HashTable应用
{
class Program
{
//------------------------------------------------------------------------
//HashTable应用
//哈希表之电话本
//包括一个键值(hash),和值,一一对应----读出来也要类型转换
//电话本应用--自己优化
static void Main(string[] args)
{
//实例化集合对象
Hashtable ht = new Hashtable();
while (true)
{
//界面
Console.WriteLine("\n==========请选择操作==========");
Console.WriteLine("1、添加联系人 2、查找 3、显示");
Console.WriteLine("==============================");
//用户选择
string input = Console.ReadLine();//定义字符串不用类型转换
//判断用户的选择
switch (input)
{
case "1":
Console.WriteLine("请输入联系人姓名:");
string name = Console.ReadLine();
//是否已经存在此姓名
if (ht.ContainsKey(name))//判断给的值在不在集合中间
{
Console.WriteLine("该联系人已存在!");
}
else//如果不存在就添加
{
Console.WriteLine("请输入联系人的电话:");
string tel = Console.ReadLine();
//添加到哈希表中保存
ht.Add(name, tel);//账号名(键值),电话()
Console.WriteLine("添加成功");
Console.WriteLine("=====共有联系人:{0}=====", ht.Count);
}
break;
case "2"://(查询在不在电话本中)
Console.WriteLine("请输入要查询的联系人姓名:");
string finaName = Console.ReadLine();
if (ht.ContainsKey(finaName))
{
Console.WriteLine("你所查找的联系人电话为:{0}", ht[finaName]);
}
else
{
Console.WriteLine("该联系人不存在!");
}
break;
case "3"://遍历一下,然后显示
foreach (string s in ht.Keys)//遍历键值(姓名)
{
Console.WriteLine("姓名:{0} 电话:{1}", s, ht[s]);
}
break;
default: Console.WriteLine("输入错误,请重新输入!"); break;
}
}
//程序等待
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace List泛型应用
{
//------------------------------------------------------------------------
//List泛型应用
class Program
{
//类
class stu
{
//字段
private string name;
private int id;
//属性
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
//有参数构造方法
public stu(string name, int id)
{
this.name = name;
this.Id = id;
}
//输出方法
public void say()
{
Console.WriteLine("姓名为{0},学号为{1}", name, Id);
}
}
static void Main(string[] args)
{
//实例化泛型集合对象
List<stu> lstStu = new List<stu>();
//实例化类对象
stu s1 = new stu("张三", 1);
stu s2 = new stu("李四", 2);
stu s3 = new stu("王五", 3);
stu s4 = new stu("陆六", 4);
//添加到泛型集合里
lstStu.Add(s1);
lstStu.Add(s2);
lstStu.Add(s3);
lstStu.Add(s4);
//输出
Console.WriteLine("第一种输出方式:foreach循环");
foreach (stu s in lstStu)
{
s.say();//不用类型转换
}
Console.WriteLine("----------------------------------");
Console.WriteLine("第二种输出方式:ForEach()方法");
//ForEach()方法可以使用委托对集合的每一个成员进行操作
//delegate(参数) 语句;
lstStu.ForEach(
delegate(stu s) { s.say(); }
);//这是方法,有委托事件(用于调用),要调用的方法
Console.WriteLine("----------------------------------");
//FindAll()方法检索与指定谓词所定义的条件相匹配的所有元素
//它是委托中可以带函数的了,
List<stu> ls = lstStu.FindAll(//定义新的泛型来
delegate(stu s)
{
if (s.Id < 3)//学号是否小于三
{
return true;
}
else
{
return false;
}
}
);
foreach (stu s in ls)
{
s.say();
}
//程序等待
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//arry是集合的基类所以不用引用内存(空间)
namespace x1808_5_21_集合和泛型
{
class Program
{
static void Main(string[] args)
{
Array arr = Array.CreateInstance(typeof(string), 5);
//只能调用方法setvalue设置值getvalue得到值
/*array(数组)是c#集合的基类可以定义多维数据,缺点是静态分布,固定长度*/
arr.SetValue("金庸", 0);
arr.SetValue("古龙", 1);
arr.SetValue("黄易", 2);
arr.SetValue("梁羽生", 3);
arr.SetValue("诸葛青云", 4);
Console.WriteLine("数组元素总个数:" + arr.Length);
Console.WriteLine("\n数组元素是:");
/*得到arry属性的*/
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("第{0}个元素是:{1}", i + 1, arr.GetValue(i));
}
/*sort排序的方法,下面是字符串排序,翻译为拼音后首字母比较ascii码,次字母比较*/
Array.Sort(arr);
/*输出排序后的元素*/
Console.WriteLine("\n排序后数组为:");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("第{0}个元素是:{1}", i + 1, arr.GetValue(i));
}
//程序等待
Console.ReadLine();
}
}
}