using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
public class CStringList
{
/// <summary>
/// 数组增值数
/// </summary>
public const int IncLength = 20;
/// <summary>
/// 字符串数组
/// </summary>
protected string[] list;
/// <summary>
/// 字符串个数
/// </summary>
protected int count;
/// <summary>
/// 空参构造方法
/// </summary>
public CStringList()
{
this.list = new string[IncLength];
this.count = 0;
}
/// <summary>
/// 以任意个(小于20)字符串参数构造对象的构造方法
/// </summary>
/// <param name="items">变长形参</param>
public CStringList(params string[] items)
{
list = new string[IncLength];
this.count = 0;
if (items != null && items.Length > 0)
{
int maxLength = 0;
if (items.Length > IncLength)
{
maxLength = IncLength;
}
else
{
maxLength = items.Length;
}
for (int i = 0; i < maxLength; i++)
{
this.list[i] = items[i];
}
this.count = maxLength;
}
}
/// <summary>
/// 在不改变集合当前内容的情况下,使集合容量增加IncLength
/// </summary>
protected void Expend()
{
int newLength = this.list.Length + IncLength;
string[] newList = new string[newLength];
for (int i = 0; i < this.count; i++)
{
newList[i] = this.list[i];
}
this.list = null;
this.list = newList;
}
/// <summary>
/// 只读属性,返回当前集合中的元素个数
/// </summary>
public int Count
{
get
{
return this.count;
}
}
/// <summary>
/// 在集合尾部追加一个元素,若失败抛出Exception
/// </summary>
/// <param name="item">新加入的元素</param>
virtual public void Add(string item)
{
if (this.count == this.list.Length)
{
this.Expend();
}
this.list[this.count] = item;
this.count++;
}
/// <summary>
/// 在集合尾部追加任意个元素,若失败抛出Exception
/// </summary>
/// <param name="items">变长形参</param>
virtual public void Add(params string[] items)
{
if (this.count + items.Length > this.list.Length)
{
this.Expend();
}
for (int i = 0; i < items.Length; i++)
{
this.list[this.count + i] = items[i];
}
this.count += items.Length;
}
/// <summary>
/// 在集合指定位置插入一个元素,若失败抛出Exception
/// </summary>
/// <param name="item">被插入的元素</param>
/// <param name="index">插入位置</param>
virtual public void Insert(string item, int index)
{
if (index < 0)
{
throw new Exception("索引值小于零。");
}
if (this.count == this.list.Length)
{
this.Expend();
}
if (index > this.count)
{
throw new Exception("索引值超出数组长度。");
}
for (int i = this.count; i > index; i--)
{
this.list[i] = this.list[i - 1];
}
this.list[index] = item;
this.count++;
}
/// <summary>
/// 移除指定位置的元素,若失败抛出Exception
/// </summary>
/// <param name="Index"></param>
public void RemoveAt(int Index)
{
if (Index < 0)
{
throw new Exception("索引值小于零。");
}
if (Index >= this.count)
{
throw new IndexOutOfRangeException("索引值大于数组长度。");
}
for (int i = Index; i < this.count - 1; i++)
{
this.list[i] = this.list[i + 1];
}
this.list[this.count - 1] = null;
this.count--;
}
/// <summary>
/// 清空集合所有元素
/// </summary>
public void Clear()
{
this.count = 0;
this.list = new string[IncLength];
}
/// <summary>
/// 查找指定字符串aString在集合中的位置(大小写敏感)
/// </summary>
/// <param name="item">被查找的字符串</param>
/// <returns>找到则返回对应位置,未找到返回-1</returns>
virtual public int Find(string aString)
{
for (int i = 0; i < this.count; i++)
{
if (this.list[i] != null && this.list[i] == aString)
{
return i;
}
}
return -1;
}
/// <summary>
/// 对集合进行排序
/// </summary>
/// <param name="Opt">参数=asc为正向排序,=desc为逆向排序</param>
public void Sort(string Opt = "asc")
{
if (Opt.ToLower() == "asc")
{
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (this.list[i].CompareTo(this.list[j]) > 0)
{
string temp = string.Empty;
temp = this.list[i];
this.list[i] = this.list[j];
this.list[j] = temp;
}
}
}
}
else
{
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (this.list[i].CompareTo(this.list[j]) < 0)
{
string temp = string.Empty;
temp = this.list[i];
this.list[i] = this.list[j];
this.list[j] = temp;
}
}
}
}
}
/// <summary>
/// 只读属性,返回集合中字符串长度最大的一项,若存在多项长度相等返回其中的第一项
/// </summary>
public string MaxLenItem
{
get
{
if (this.count == 0)
{
return null;
}
if (this.count == 1)
{
return this.list[0];
}
int index = 0;
for (int i = 1; i < this.count; i++)
{
if (this.list[i].Length > this.list[i - 1].Length)
{
index = i;
}
}
return this.list[index];
}
}
/// <summary>
/// 只读属性,返回集合中字符串长度最小的一项,若存在多项长度相等返回其中的第一项
/// </summary>
public string MinLenItem
{
get
{
if (this.count == 0)
{
return null;
}
if (this.count == 1)
{
return this.list[0];
}
int index = 0;
for (int i = 1; i < this.count; i++)
{
if (this.list[i].Length < this.list[i - 1].Length)
{
index = i;
}
}
return this.list[index];
}
}
/// <summary>
/// 按位置输出集合中的各项元素,输出格式为:
///
/// 集合中共有?个元素,分别为:
/// 元素1
/// 元素2
/// ……
/// 元素n
/// </summary>
public void Show()
{
if (this.count == 0)
{
Console.WriteLine("集合中没有元素。");
return;
}
Console.WriteLine("集合中共有{0}个元素,分别为:", this.count);
for (int i = 0; i < this.count; i++)
{
Console.WriteLine("元素{0}:{1}", i, this.list[i]);
}
}
}
public class CMyDirectory : CStringList
{
/// <summary>
/// 检查字符串数组参数strArr中是否包含重复数据项(大小写不敏感)
/// </summary>
/// <param name="strArr">被检查的字符串数据</param>
/// <returns>true:不包含重复项;false:包含重复项</returns>
public bool IsValid(string[] strArr)
{
if (strArr.Length <= 1)
{
return false;
}
List<string> list = new List<string>();
list.Add(strArr[0]);
for (int i = 1; i < strArr.Length; i++)
{
foreach (var s in list)
{
if (s.ToLower() == strArr[i].ToLower())
{
return true;
}
}
list.Add(strArr[i]);
}
return false;
}
/// <summary>
/// 集合尾部添加一个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="item">追加的新元素</param>
public override void Add(string item)
{
bool contained = false;
if (this.count == this.list.Length)
{
this.Expend();
}
for (int i = 0; i < this.count; i++)
{
if (this.list[i].ToLower() == item.ToLower())
{
contained = true;
break;
}
}
if (!contained)
{
base.Add(item);
}
}
/// <summary>
/// 集合尾部添加若干个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="items">追加的新元素</param>
public override void Add(params string[] items)
{
if (this.count + items.Length > this.list.Length)
{
this.Expend();
}
for (int i = 0; i < items.Length; i++)
{
if (this.count == 0) {
base.Add(items[i]);
continue;
}
bool contained = false;
for (int j = 0; j < this.count; j++)
{
if (this.list[j].ToLower() == items[i].ToLower())
{
contained = true;
break;
}
}
if (!contained)
{
base.Add(items[i]);
}
}
}
/// <summary>
/// 在指定的位置插入一个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="item">插入的新元素</param>
/// <param name="index">插入位置</param>
public override void Insert(string item, int index)
{
bool contained = false;
if (this.count == this.list.Length)
{
this.Expend();
}
for (int i = 0; i < this.count; i++)
{
if (this.list[i].ToLower() == item.ToLower())
{
contained = true;
break;
}
}
if (!contained)
{
base.Insert(item, index);
}
}
/// <summary>
/// 查找一个指定元素在集合中的位置(大小写不敏感)。
/// </summary>
/// <param name="aString"></param>
/// <returns></returns>
public override int Find(string aString)
{
for (int i = 0; i < this.count; i++)
{
if (this.list[i].ToLower() == aString.ToLower())
{
return i;
}
}
return -1;
}
}
}
C#一个数组的实现
最新推荐文章于 2022-08-28 16:40:26 发布