当前位置: 移动技术网 > IT编程>开发语言>.net > 集合类Array List HashTable实例操作练习

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

伊丽莎白镇插曲,上海宝山邮编,弹幕t恤有几种款式

集合常用操作添加、遍历、移除
命名空间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();
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网