没有合适的资源?快使用搜索试试~ 我知道了~
C#自建类实现集合接口IEnumerable, IEnumerator
4星 · 超过85%的资源 需积分: 35 11 下载量 77 浏览量
2013-01-11
14:53:27
上传
评论 1
收藏 5KB TXT 举报
温馨提示
试读
7页
C#自建集合类MyArrayList实现集合接口IEnumerable, IEnumerator, 实现方法 inr add(object value),void Remove(object o), int Count, void Clear()
资源推荐
资源详情
资源评论
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
namespace MyConProject
{
/// <summary>
/// C#自建集合类MyArrayList实现集合接口IEnumerable, IEnumerator
/// 实现方法 inr add(object value),void Remove(object o), int Count, void Clear
/// </summary>
class MyArrayList : IEnumerable, IEnumerator
{
private object[] obj; //初始化保存集合元素的object数组
private int count = 0; //初始化数组个数0
private int index = -1;//初始化数组下标为-1,避免一开始下标移动出错
/// <summary>
/// 返回集合内元素的总个数
/// </summary>
public int Count
{
get { return count; }
}
/// <summary>
/// 无参构造,初始化数组个数0
/// </summary>
public MyArrayList()
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
namespace MyConProject
{
/// <summary>
/// C#自建集合类MyArrayList实现集合接口IEnumerable, IEnumerator
/// 实现方法 inr add(object value),void Remove(object o), int Count, void Clear
/// </summary>
class MyArrayList : IEnumerable, IEnumerator
{
private object[] obj; //初始化保存集合元素的object数组
private int count = 0; //初始化数组个数0
private int index = -1;//初始化数组下标为-1,避免一开始下标移动出错
/// <summary>
/// 返回集合内元素的总个数
/// </summary>
public int Count
{
get { return count; }
}
/// <summary>
/// 无参构造,初始化数组个数0
/// </summary>
public MyArrayList()
{
obj = new object[count];
}
/// <summary>
/// 有参构造,指定初始化数组个数
/// </summary>
/// <param name="index"></param>
public MyArrayList(int index)
{
obj = new object[index];
}
/// <summary>
/// 新增功能,添加到数组末尾处,返回数组下标
/// </summary>
/// <param name="obj">要新增的对象</param>
/// <returns></returns>
public int add(object o)
{
object[] old = obj;
if (count >= obj.Length)
{
obj = new object[old.Length + 1];
for (int i = 0; i < old.Length; i++)
{
obj[i] = old[i];
}
}
obj[count] = o;
count++;
obj = new object[count];
}
/// <summary>
/// 有参构造,指定初始化数组个数
/// </summary>
/// <param name="index"></param>
public MyArrayList(int index)
{
obj = new object[index];
}
/// <summary>
/// 新增功能,添加到数组末尾处,返回数组下标
/// </summary>
/// <param name="obj">要新增的对象</param>
/// <returns></returns>
public int add(object o)
{
object[] old = obj;
if (count >= obj.Length)
{
obj = new object[old.Length + 1];
for (int i = 0; i < old.Length; i++)
{
obj[i] = old[i];
}
}
obj[count] = o;
count++;
剩余6页未读,继续阅读
资源评论
- sgliky2013-10-19挺好的。把IEnumable当成LIST来用了。
吴丘
- 粉丝: 7
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功