一、非泛型实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ArrayList ary = new ArrayList(10);
ary.Add(3);
ary.Add(5);
ary.Add(4, 1);
for (int i = 0; i < ary.count; i++)
{
Console.WriteLine(ary[i]);
}
ary.Remove(4);
for (int i = 0; i < ary.count; i++)
{
Console.WriteLine(ary[i]);
}
ary.RemoveAt(1);
for (int i = 0; i < ary.count; i++)
{
Console.WriteLine(ary[i]);
}
}
}
/// <summary>
/// 线性表的顺序结构基本实现(非泛型)
/// </summary>
public class ArrayList
{
/// <summary>
/// 存放元素的数组
/// </summary>
public object[] array;
/// <summary>
/// 存放实际的元素个数
/// </summary>
public int count;
/// <summary>
/// 存放线性表的容量
/// </summary>
public int capcity;
/// <summary>
/// 默认的构造函数
/// </summary>
public ArrayList()
{
this.array = new object[0];
this.capcity = 0;
this.count = 0;
}
/// <summary>
/// 构造函数重载
/// </summary>
/// <param name="capcity"></param>
public ArrayList(int capcity)
{
this.array = new object[capcity];
this.capcity = capcity;
this.count = 0;
}
/// <summary>
/// 判断是否为空
/// </summary>
/// <returns></returns>
private bool Empty()
{
return this.count == 0; //元素个数是否等于0
}
/// <summary>
/// 判断是否为满
/// </summary>
/// <returns></returns>
private bool Fill()
{
return this.count == this.capcity;//元素个数是否等于线性表的容器
}
/// <summary>
/// 索引
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public object this[int index]
{
get
{
if (index < 0 || index > this.count)
{
return "";
}
else
{
return this.array[index];
}
}
set
{
if (index < 0 || index > this.count)
{
this.array[index] = "";
}
else
{
this.array[index] = value;
}
}
}
/// <summary>
/// 扩大容器
/// </summary>
private void Dilatation()
{
//将数组个数扩大为原来的两倍
object[] newarray = new object[this.capcity * 2];
for (int i = 0; i < this.array.Length; i++)
{
newarray[i] = this.array[i];
}
this.array = newarray;
}
/// <summary>
/// 增加元素
/// </summary>
/// <param name="obj"></param>
public void Add(object obj)
{
//判断是否为满,满的话扩容,最后就是添加元素
if (Fill())
{
Dilatation();
}
this.array[count] = obj;
this.count++;
}
/// <summary>
/// 增加元素到指定的位置
/// </summary>
/// <param name="obj"></param>
/// <param name="index"></param>
public void Add(object obj, int index)
{
if (Fill())
{
Dilatation();
}
if (index < 0 || index > this.count)
{
Console.WriteLine("插入位置不正确");
return;
}
//位置后移
for (int i = index; i < this.count; i++)
{
this.array[i + 1] = this.array[i];
}
this.array[index] = obj;
this.count++;
}
/// <summary>
/// 查找元素是否存在
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Contains(object obj)
{
bool b = false;
for (int i = 0; i < this.count; i++)
{
if (this.array[i].Equals(obj))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 查找元素的位置,如果不存在返回-1
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int IndexOf(object obj)
{
int i = -1;
for (int j = 0; j < this.count; j++)
{
if (this.array[j].Equals(obj))
{
i = j;
break;
}
}
return i;
}
/// <summary>
/// 删除元素
/// </summary>
/// <param name="obj"></param>
public void Remove(object obj)
{
int index = IndexOf(obj);
if (index != -1)
{
//位置前移
for (int i = index; i < this.count - 1; i++)
{
this.array[i] = this.array[i + 1];
}
this.count--;
}
else
{
Console.WriteLine("该元素不存在");
}
}
/// <summary>
/// 删除指定位置的元素
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
if (this.Empty())
{
Console.WriteLine("元素为空");
}
if (index < 0 || index > this.count)
{
Console.WriteLine("删除位置不正确");
return;
}
//位置前移
for (int i = index; i < this.count - 1; i++)
{
this.array[i] = this.array[i + 1];
}
this.count--;
}
}
}
二、泛型实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ArrayList<int> ary = new ArrayList<int>(10);
ary.Add(3);
ary.Add(5);
ary.Add(4, 1);
for (int i = 0; i < ary.count; i++)
{
Console.WriteLine(ary[i]);
}
ary.Remove(4);
for (int i = 0; i < ary.count; i++)
{
Console.WriteLine(ary[i]);
}
ary.RemoveAt(1);
for (int i = 0; i < ary.count; i++)
{
Console.WriteLine(ary[i]);
}
}
}
/// <summary>
/// 线性表的顺序结构基本实现(泛型)
/// </summary>
public class ArrayList<T>
{
/// <summary>
/// 存放元素的数组
/// </summary>
public T[] array;
/// <summary>
/// 存放实际的元素个数
/// </summary>
public int count;
/// <summary>
/// 存放线性表的容量
/// </summary>
public int capcity;
/// <summary>
/// 默认的构造函数
/// </summary>
public ArrayList()
{
this.array = new T[0];
this.capcity = 0;
this.count = 0;
}
/// <summary>
/// 构造函数重载
/// </summary>
/// <param name="capcity"></param>
public ArrayList(int capcity)
{
this.array = new T[capcity];
this.capcity = capcity;
this.count = 0;
}
/// <summary>
/// 判断是否为空
/// </summary>
/// <returns></returns>
private bool Empty()
{
return this.count == 0; //元素个数是否等于0
}
/// <summary>
/// 判断是否为满
/// </summary>
/// <returns></returns>
private bool Fill()
{
return this.count == this.capcity;//元素个数是否等于线性表的容器
}
/// <summary>
/// 索引
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
if (index < 0 || index > this.count)
{
new System.ArgumentOutOfRangeException();
}
return this.array[index];
}
set
{
if (index < 0 || index > this.count)
{
new System.ArgumentOutOfRangeException();
}
this.array[index] = (T)value;
}
}
/// <summary>
/// 扩大容器
/// </summary>
private void Dilatation()
{
//将数组个数扩大为原来的两倍
T[] newarray = new T[this.capcity * 2];
for (int i = 0; i < this.array.Length; i++)
{
newarray[i] = this.array[i];
}
this.array = newarray;
}
/// <summary>
/// 增加元素
/// </summary>
/// <param name="obj"></param>
public void Add(T obj)
{
//判断是否为满,满的话扩容,最后就是添加元素
if (Fill())
{
Dilatation();
}
this.array[count] = obj;
this.count++;
}
/// <summary>
/// 增加元素到指定的位置
/// </summary>
/// <param name="obj"></param>
/// <param name="index"></param>
public void Add(T obj, int index)
{
if (Fill())
{
Dilatation();
}
if (index < 0 || index > this.count)
{
Console.WriteLine("插入位置不正确");
return;
}
//位置后移
for (int i = index; i < this.count; i++)
{
this.array[i + 1] = this.array[i];
}
this.array[index] = obj;
this.count++;
}
/// <summary>
/// 查找元素是否存在
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Contains(T obj)
{
bool b = false;
for (int i = 0; i < this.count; i++)
{
if (this.array[i].Equals(obj))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 查找元素的位置,如果不存在返回-1
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int IndexOf(T obj)
{
int i = -1;
for (int j = 0; j < this.count; j++)
{
if (this.array[j].Equals(obj))
{
i = j;
break;
}
}
return i;
}
/// <summary>
/// 删除元素
/// </summary>
/// <param name="obj"></param>
public void Remove(T obj)
{
int index = IndexOf(obj);
if (index != -1)
{
//位置前移
for (int i = index; i < this.count - 1; i++)
{
this.array[i] = this.array[i + 1];
}
this.count--;
}
else
{
Console.WriteLine("该元素不存在");
}
}
/// <summary>
/// 删除指定位置的元素
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
if (this.Empty())
{
Console.WriteLine("元素为空");
}
if (index < 0 || index > this.count)
{
Console.WriteLine("删除位置不正确");
return;
}
//位置前移
for (int i = index; i < this.count - 1; i++)
{
this.array[i] = this.array[i + 1];
}
this.count--;
}
}
}