集合类Array List HashTable实例操作练习

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了集合类Array List HashTable实例操作练习脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
集合常用操作添加、遍历、移除
命名空间System.Collections

ArrayList 可变长度数组,使用类似于数组
属性 CapacITy Count
方法
Add() AddRange() Remove() RemoveAt() Clear()
Contains() ToArray()
Hashtable 键值对(KeyValuePair)的集合,类似于字典

a、ArrayList对值类型的操作
复制代码 代码如下:

using System;
using System.Collections;
namespace _08_ArrayList {
//ArayList对值类型的操作
class PRogram {
static void Main( string[] args) {
//ArrayList与数组没多大的区别 优点在于不像数组需规定长度 缺点是数据类型不限制 什么类型数据都可以放入 这样会出现许多错误
ArrayList arylist = new ArrayList();
//ArrayList添加
arylist.Add(1000);
//arylist.Add("张三");//参数类型为object 所以可以添加多种类型的参数 取出时同样需要类型转换
arylist.Add(3000);
arylist.Add(4000); //发生装箱操作 将值类型转换引用类型
arylist.Add(5000);
int [] arr = { 1, 2, 3, 4 };
arylist.AddRange(arr); //AddRange参数是实现了ICollections接口的对象 可以一次性添加数组、array、ArrayList等实现接口的对象
//集合中元素个数 使用Count = 数组Length
Console .WriteLine("集合内容长度" + arylist.Count);
//Capacity为集合的容量 是可变的 一般*2增长
Console .WriteLine(arylist.Capacity);
//访问集合第一个元素
int Firstlist = Convert .ToInt32(arylist[0]);
Console .WriteLine(firstlist.ToString());
//ArrayList遍历
int sum2 = 0;
for (int i = 0; i < arylist.Count; i++) {
//sum2 += Convert.ToInt32(arylist[i]);//发生拆箱操作
Console .WriteLine(arylist[i].ToString());
}
foreach (object item in arylist) {
sum2 += Convert .ToInt32(item);
}
Console .WriteLine(sum2);
//ArrayList移除 只是移除 不是删除
arylist.Remove(1000); //移除内容是1000的 Remove移除内部的某个对象
arylist.RemoveAt(1); //移除第二项 按索引移除
//注意 移除元素 ArrayList数组会重新分配索引 所以移除操作最好是倒叙移除元素
//如果移除所有的元素 直接使用Clear
//arylist.Clear();
if (arylist.Contains(3000)) {
Console .WriteLine("包含" );
}
//ArrayList还有ToArray()但是意义不大
//这里是在ArrayList中添加值类型 那么引用类型呢????添加Student类的对象?
Console .Read();
}
}
}

b、ArrayList对引用类型的操作
复制代码 代码如下:

using System;
using System.Collections;
namespace _09_ArrayListObject {
//ArrayList对引用类型的操作
class Student {
public Student(string name, int age) {
this .name = name;
this .age = age;
}
private string name;
public string Name {
get {
return name;
}
set {
name = value ;
}
}
private int age;
public int Age {
get {
return age;
}
set {
age = value ;
}
}
}
class Program {
static void Main( string[] args) {
Student xyy = new Student( "小月月" , 14);
Student fj = new Student( "凤姐" , 18);
Student fr = new Student( "芙蓉姐姐" , 19);
Student xl = new Student( "犀利哥" , 20);
ArrayList student = new ArrayList();
student.Add(xyy); //添加 也可以使用AddRange
student.Add(fj);
student.Add(fr);
student.Add(xl);
//移除
//student.Remove(fj);//这里移除的就是对象 而不是值
//student.RemoveAt(1);//索引移除
//移除不掉fj 因为Remove后是object 按索引移除
//Student stu = new Student("凤姐", 18);
//student.Remove(stu);
//Console.WriteLine(student.Contains(stu));//false 通过索引检索 因为stu与fj地址是不一样的
//遍历
for (int i = 0; i < student.Count; i++) {
Student s = student[i] as Student; //因为添加前发生装箱操作 所以 现在需要拆箱 student[i]是不能点出name的
Console .WriteLine(s.Name);
}
ArrayList ary = new ArrayList();
ary.Add( "凤姐" );
ary.Add( "小月月" );
//string类同样是引用类型 但是这里有些特别
string name = "凤姐" ;
Console .WriteLine(ary.Contains(name));//string比较的是内容 所以返回true
//根据学生姓名 获取学生对象 虽然ArrayList可以实现 但是相当的复杂 而且效率低下 所以接下来学习HashTable
Console .Read();
}
}
}

c、HashTable
复制代码 代码如下:

using System;
using System.Collections;
namespace _10_HashTable {
class Student {
public Student(string name, int age) {
this .name = name;
this .age = age;
}
private string name;
public string Name {
get {
return name;
}
set {
name = value ;
}
}
private int age;
public int Age {
get {
return age;
}
set {
age = value ;
}
}
}
class Program {
static void Main( string[] args) {
//仍然使用Student的类来实现
//Hashtable键值对形式 key value 相当于字典 能根据学生的姓名快速的找到对象
Student xyy = new Student( "小月月" , 14);
Student fj = new Student( "凤姐" , 18);
Student fr = new Student( "芙蓉姐姐" , 19);
Student xl = new Student( "犀利哥" , 20);
Hashtable student = new Hashtable();
student.Add( "小月月" , xyy);
student.Add( "凤姐" , fj);
student.Add( "芙蓉姐姐" , fr);
student.Add( "犀利哥" , xl);
//student.Add("犀利哥",xl);//错误 字典中的关键字key 不允许重复 所以不能再添加犀利哥
//移除 因为没有索引 所以没有RemoveAt()
student.Remove( "小月月" );//根据key来移除
student.Clear();
student.ContainsKey( "凤姐" );//判断是不是含有这个键
//遍历 因为字典没有索引 所以不能使用for来遍历 只能使用foreach
//按key遍历 经常用
foreach (object key in student.Keys) {
Student stu = student[key] as Student;
Console .WriteLine(key);
Console .WriteLine(stu.Age);
}
//按value遍历
foreach (object value in student.Values) {
Student stu = value as Student;
if (stu != null ) {
Console .WriteLine(stu.Age);
}
}
//如果不按key 也不按value遍历 对字典遍历就是对字典的键值对进行遍历
foreach (DictionaryEntry de in student) {
Console .WriteLine(de.Key);
Student s = de.Value as Student; //因为得到的是object类型 所以 还需要转换才可以使用
Console .WriteLine(s.Age);
}
Student s2 = student["小月月" ] as Student ;//通过姓名找到该对象 获取其他的属性
if (s2 != null ) {
Console .WriteLine(s2.Age);
}
Console .Read();
}
}
}

d、练习
复制代码 代码如下:

using System;
using System.Collections;
namespace _11_ArrayList练习 {
class Program {
//还是那句话 理解题目之后 有了思路再开始写code 思路最重要
static void Main( string[] args) {
//两个集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个
ArrayList ary1 = new ArrayList { "a" , "b" , "c", "d" , "e" };
ArrayList ary2 = new ArrayList { "d" , "e" , "f", "g" , "h" };
//遍历两个集合
for (int i = 0; i < ary2.Count; i++) { //循环遍历ary2元素与ary1逐个比较 如果存在相同值 则不添加 否则追加到ary1中
if (!ary1.Contains(ary2[i])) {//有Contains方法 如果没有 不知道有多复杂
ary1.Add(ary2[i]);
}
}
foreach (object item in ary1) {
Console .Write(item);
}
//随机生成10个1-100之间的数放到ArrayList中,要求这10个数不能重复,并且都是偶数
ArrayList arylist = new ArrayList();
//int numCount = 0;
while (true ) {
Random ran = new Random();
int num = ran.Next(1, 100);
if (num % 2 == 0 && !arylist.Contains(num)) { //添加!arylist.Contains(num)这句话 解决以下问题
arylist.Add(num); //为什么直接运行总显示第一个满足条件数值 而单步调试却显示正确结果???
}
if (arylist.Count == 10) {
break ;
}
}
foreach (object item in arylist) {
Console .WriteLine(item);
}
//有一个字符串是用空格分隔的一系列整数,写一个程序把其中的整数做如下重新排列打印出来:奇数显示在左侧、偶数显示在右侧。比如‘2 7 8 3 22 9'显示成‘7 3 9 2 8 22
string str = "2 7 8 3 22 9" ;
ArrayList ary3 = new ArrayList();
ArrayList ary4 = new ArrayList();
string [] s = str.Split(' ' );
foreach (VAR item in s) {
if (Convert .ToInt32(item) % 2 == 0) {
ary4.Add(item);
} else {
ary3.Add(item);
}
}
ary3.AddRange(ary4); //因为ary1类型为object 所以无法使用string类的join方法实现字符拼接 后面学过泛型集合可以处理
string newstr = ary3[0].ToString();//简单方式去掉空格
for (int i = 1; i < ary3.Count; i++) {
newstr += " " + ary3[i];
}
Console .WriteLine("原字符串:{0},筛选后的字符串{1}" , str, newstr + "test" );
Console .Read();
}
}
}

脚本宝典总结

以上是脚本宝典为你收集整理的集合类Array List HashTable实例操作练习全部内容,希望文章能够帮你解决集合类Array List HashTable实例操作练习所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。