lwj_C#_集合的使用、接口方法的实现;
来源:互联网 发布:医疗器械软件关税 编辑:程序博客网 时间:2024/05/01 00:19
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace homework
{
//枚举能不能做//枚举如何实现
public class MyList <T>:IEnumerable,IEnumerator{
//创建集合字段要初始化
public List<T> list = new List<T>();
private int currentIndex = 0;
public IEnumerator GetEnumerator()
{
return this;
}
//先走MoveNext 再走Current
//移位
public bool MoveNext()
{
if (currentIndex<list.Count)
{
return true;
}
Reset();
return false;
}
public object Current
{
get
{
Object obj = list[currentIndex];
currentIndex++;
return obj;
}
}
//重置 是用来重置循环打印次数;
public void Reset()
{
currentIndex = 0;
}
}
class MainClass
{
public static void Main(string[] args)
{
#region // A类 //第一题: 哈希表和字典存取数据所消耗的时间对比
//题目
//解题思路
//1.分别创建哈希表与字典
//2.分别添加对应的数据使用stopwatch统计耗时
//3.分别取对应的数据使用stopwatch统计耗时
//添加数据比较;
Hashtable hash = new Hashtable();
Stopwatch stop_1 = new Stopwatch();
stop_1.Start();
hash.Add("jiangkai", 10);
stop_1.Stop();
Console.WriteLine("哈希表的时间为:" + stop_1.Elapsed);
Dictionary<string, int> dic = new Dictionary<string, int>();
Stopwatch stop_2 = new Stopwatch();
stop_2.Start();
dic.Add("jiangkai", 10);
stop_2.Stop();
Console.WriteLine("字典的时间为:" + stop_2.Elapsed);
//取数据比较;
Stopwatch stop_3 = new Stopwatch();
stop_3.Start();
hash.Remove("jiangkai");
stop_3.Stop();
Console.WriteLine("哈希表的时间为:" + stop_3.Elapsed);
Stopwatch stop_4 = new Stopwatch();
stop_4.Start();
dic.Remove("jiangklai");
stop_4.Stop();
Console.WriteLine("字典的时间为:" + stop_4.Elapsed);
#endregion
#region //第二题: 查询MSDN文档,练习ArrayList集合下面的常用API练习一遍.然后思考ArrayList能用于开发中(或者你接触过的游戏中)的那些地方? 至少写出两个, 并能分析出其实现方式.
ArrayList arr = new ArrayList();
arr.Add("lwj");
arr.Add(12);
arr.Add('a');
foreach (var item in arr)
{
Console.Write(item + " ");
}
//指定下标插入元素;
arr.Insert(1, "niubi");
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//是否包含一个指定元素
bool isexit = arr.Contains(12);
Console.WriteLine("\n" + isexit);
//逆转
arr.Reverse();
foreach (var item in arr)
{
Console.Write(item + " ");
}
//移除指定下标元素;
arr.RemoveAt(2);
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//移除指定元素
arr.Remove("lwj");
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//移除所有元素
arr.Clear();
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//ArrayList能用于:1、游戏中的技能BUFF移除、添加等操作; 2、游戏物品栏的添加移除操作;
#endregion
//解题思路
//1.自定义类拥有集合的作用,意味着类成员至少有一个为系统提供的集合
//2.利用系统集合的方法,使用本类进行二次封装
//3.类支持foreach循环,必须实现IEnumerable接口
//4.利用里氏替换原则替换父类实现IEnumerator
//5.创建测试类
//6.利用自定类内部的索引器添加测试类对象
//7.测试foreach循环
int[] array = { 12, 232, 343, 2322 };
MyList<int> mylist = new MyList<int>();
mylist.list = new List<int>(array);
foreach (var item in mylist)
{
Console.WriteLine(item);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace homework
{
//枚举能不能做//枚举如何实现
public class MyList <T>:IEnumerable,IEnumerator{
//创建集合字段要初始化
public List<T> list = new List<T>();
private int currentIndex = 0;
public IEnumerator GetEnumerator()
{
return this;
}
//先走MoveNext 再走Current
//移位
public bool MoveNext()
{
if (currentIndex<list.Count)
{
return true;
}
Reset();
return false;
}
public object Current
{
get
{
Object obj = list[currentIndex];
currentIndex++;
return obj;
}
}
//重置 是用来重置循环打印次数;
public void Reset()
{
currentIndex = 0;
}
}
class MainClass
{
public static void Main(string[] args)
{
#region // A类 //第一题: 哈希表和字典存取数据所消耗的时间对比
//题目
//解题思路
//1.分别创建哈希表与字典
//2.分别添加对应的数据使用stopwatch统计耗时
//3.分别取对应的数据使用stopwatch统计耗时
//添加数据比较;
Hashtable hash = new Hashtable();
Stopwatch stop_1 = new Stopwatch();
stop_1.Start();
hash.Add("jiangkai", 10);
stop_1.Stop();
Console.WriteLine("哈希表的时间为:" + stop_1.Elapsed);
Dictionary<string, int> dic = new Dictionary<string, int>();
Stopwatch stop_2 = new Stopwatch();
stop_2.Start();
dic.Add("jiangkai", 10);
stop_2.Stop();
Console.WriteLine("字典的时间为:" + stop_2.Elapsed);
//取数据比较;
Stopwatch stop_3 = new Stopwatch();
stop_3.Start();
hash.Remove("jiangkai");
stop_3.Stop();
Console.WriteLine("哈希表的时间为:" + stop_3.Elapsed);
Stopwatch stop_4 = new Stopwatch();
stop_4.Start();
dic.Remove("jiangklai");
stop_4.Stop();
Console.WriteLine("字典的时间为:" + stop_4.Elapsed);
#endregion
#region //第二题: 查询MSDN文档,练习ArrayList集合下面的常用API练习一遍.然后思考ArrayList能用于开发中(或者你接触过的游戏中)的那些地方? 至少写出两个, 并能分析出其实现方式.
ArrayList arr = new ArrayList();
arr.Add("lwj");
arr.Add(12);
arr.Add('a');
foreach (var item in arr)
{
Console.Write(item + " ");
}
//指定下标插入元素;
arr.Insert(1, "niubi");
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//是否包含一个指定元素
bool isexit = arr.Contains(12);
Console.WriteLine("\n" + isexit);
//逆转
arr.Reverse();
foreach (var item in arr)
{
Console.Write(item + " ");
}
//移除指定下标元素;
arr.RemoveAt(2);
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//移除指定元素
arr.Remove("lwj");
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//移除所有元素
arr.Clear();
Console.WriteLine("");
foreach (var item in arr)
{
Console.Write(item + " ");
}
//ArrayList能用于:1、游戏中的技能BUFF移除、添加等操作; 2、游戏物品栏的添加移除操作;
#endregion
//解题思路
//1.自定义类拥有集合的作用,意味着类成员至少有一个为系统提供的集合
//2.利用系统集合的方法,使用本类进行二次封装
//3.类支持foreach循环,必须实现IEnumerable接口
//4.利用里氏替换原则替换父类实现IEnumerator
//5.创建测试类
//6.利用自定类内部的索引器添加测试类对象
//7.测试foreach循环
int[] array = { 12, 232, 343, 2322 };
MyList<int> mylist = new MyList<int>();
mylist.list = new List<int>(array);
foreach (var item in mylist)
{
Console.WriteLine(item);
}
}
}
}
阅读全文
0 0
- lwj_C#_集合的使用、接口方法的实现;
- lwj_C#_类的相互使用和构造方法
- lwj_C#_静态,单例的使用
- lwj_C#_建立一个数学MathTool类包含的方法
- lwj_C#_类的属性,方法参数 举例习题
- lwj_C#_接口
- lwj_C#_集合list<T>
- lwj_C#_输入年月日,写方法输出它是今年的第几天;
- lwj_C#_泛型使用
- lwj_C#_方法重载,递归,构造
- 集合框架_泛型接口的概述和使用
- lwj_C#_work 字符串、类的使用和数学运算
- lwj_C#_集合stack栈和queue队列
- Java集合对象的排序使用Collection.sort方法,先实现Comparable接口
- 集合框架_泛型方法的概述和使用
- 黑马程序员_两种遍历集合的方法Iterator接口和foreach循环
- 集合的概念、类型、接口以及接口的实现类的方法描述
- lwj_C#_排序
- 精华 | 12个关键词告诉你告诉你什么是机器学习(基础篇)
- lightoj1010 Knights in Chessboard(找规律)
- Nodejs入门实战
- [Leetcode] 192. Word Frequency
- SSH无密码登录
- lwj_C#_集合的使用、接口方法的实现;
- ESP8266--MQTT--ESP_MQTT固件开发笔记(一**)小黄板--下载最新的固件
- linux tomcat 日志乱码
- spring中@ConstructorProperties的作用
- leetcode题解-Dynamic Programming简单类别题目汇总
- String Stringbuffer
- codeforces 893E Counting Arrays (组合数学)
- 机器学习之支持向量机 (三) SVM回归模型Support Vector Regression (SVR)
- iOS-创建 tabBar(标签控制器)