1、访问修饰符。
(1)public 公共的
(2)protected 有继承关系可访问
(3)internal 本程序集可访问
(4)private 本类中可访问
(5)protected internal 本程序集或继承关系。
2、构造函数与析构函数
(1)构造函数与类名相同,不能书写返回值。
(2)析构函数自动调用,释放对象。一般不用。
3、字段
字段一般是private。
4、属性
get 属性访问器用于返回属性值,而 set 访问器用于分配新值。这些访问器可以有不同的访问级别
自己编写的ArrayList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyArrayList MyArri = new MyArrayList();
MyArri.Add(1);
MyArri.Add(2);
MyArri.Add(3);
MyArri.Add(4);
MyArri.Add(5);
MyArri.Add(6);
MyArri.Add(7);
MyArri.Add(8);
MyArri.Insert(2, 9);
MyArri.RemoveAt(7);
MyArri.Reverse(3, 4);
Console.WriteLine( MyArri.Contains(1));
foreach (object o in MyArri)
{
Console.WriteLine(o);
}
Console.WriteLine(MyArri.Capacity);
Console.ReadLine();
}
}
class MyArrayList : IEnumerable
{
object[] ObjArr = new object[0];
public object this[int index]
{
get
{
if (index < count)
{
return ObjArr[index];
}
else
{
throw new Exception("超出索引");
}
}
set
{
if (index < count)
{
ObjArr[index] = value;
}
else
{
throw new Exception("超出索引");
}
}
}
/// <summary>
/// 无参构造函数
/// </summary>
public MyArrayList()
{
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="capacity">容量值</param>
public MyArrayList(int capacity)
{
ObjArr = new object[capacity];
}
int count;
public int Count
{
get
{
return count;
}
}
/// <summary>
/// 容量
/// </summary>
public int Capacity
{
get
{
return ObjArr.Length;
}
set
{
if (value < count)
{
throw new Exception("容量小于元素个数");
}
else
{
object[] newobjarr = new object[value];
for (int i = 0; i < count; i++)
{
newobjarr[i] = ObjArr[i];
}
ObjArr = newobjarr;
}
}
}
/// <summary>
/// 添加元素
/// </summary>
/// <param name="value">元素值</param>
/// <returns></returns>
public int Add(object value)
{
if (ObjArr.Length == 0)
{
ObjArr = new object[4];
ObjArr[0] = value;
count++;
return 0;
}
else
{
if (count < ObjArr.Length)
{
ObjArr[count] = value;
count++;
return count - 1;
}
else
{
object[] newobjarr = new object[ObjArr.Length * 2];
for (int i = 0; i < ObjArr.Length; i++)
{
newobjarr[i] = ObjArr[i];
}
newobjarr[count] = value;
count++;
ObjArr = newobjarr;
return count - 1;
}
}
}
/// <summary>
/// 移除元素
/// </summary>
/// <param name="value">元素值</param>
public void Remove(object value)
{
int index = -1;
for (int i = 0; i < count; i++)
{
if (value.Equals(ObjArr[i]))
{
index = i;
break;
}
}
if (index != -1)
{
for (int i = index; i < count - 1; i++)
{
ObjArr[i] = ObjArr[i + 1];
}
ObjArr[count - 1] = null;
count--;
}
}
/// <summary>
/// foreach循环
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
for (int i = 0; i < count; i++)
{
yield return ObjArr[i];
}
}
/// <summary>
/// 清空元素
/// </summary>
public void Clear()
{
count = 0;
}
/// <summary>
/// 移除特定下标的元素
/// </summary>
/// <param name="index">下标</param>
public void RemoveAt(int index)
{
if (index < count && index >= 0)
{
for (int i = index; i < count; i++)
{
if (i != count-1)
{
ObjArr[i] = ObjArr[i + 1];
}
else
{
ObjArr[i] = null;
}
}
count--;
}
else
{
throw new Exception("数组越界!");
}
}
/// <summary>
/// 元素全部反转
/// </summary>
public void Reverse()
{
object[] NewObjArr=new object[ObjArr.Length];
for (int i = 0; i < count; i++)
{
NewObjArr[i] = ObjArr[count-1-i];
}
ObjArr = NewObjArr;
}
/// <summary>
/// 指定位置反转指定的个数
/// </summary>
/// <param name="index">指定下标</param>
/// <param name="number">反转个数</param>
public void Reverse(int index,int number)
{
object[] NewObjArr = new object[ObjArr.Length];
if (index + number <= count)
{
for (int j = 0; j < count; j++)
{
NewObjArr[j] = ObjArr[j];
}
for (int i = index; i < index + number; i++)
{
NewObjArr[i] = ObjArr[index + number + index - i - 1];
}
ObjArr = NewObjArr;
}
else
{
throw new Exception("数组越界!");
}
}
/// <summary>
/// 判断是否包含指定元素
/// </summary>
/// <param name="value">指定元素</param>
/// <returns></returns>
public bool Contains(object value)
{
for (int i = 0; i < count; i++)
{
if (value.Equals(ObjArr[i]))
{
return true;
}
}
return false;
}
/// <summary>
/// 指定位置插入指定元素
/// </summary>
/// <param name="index">指定下标</param>
/// <param name="value">指定元素</param>
public void Insert(int index, object value)
{
Add(value);
for (int i = count; i >= index; i--)
{
ObjArr[i] = ObjArr[i - 1];
}
ObjArr[index] = value;
}
}
}