using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class CStringList
{
public const int IncLength = 20;
protected string[] list;
protected int count;
/// <summary>
/// 空参构造方法
/// </summary>
public CStringList()
{
} /// <summary>
/// 以任意个(小于20)字符串参数构造对象的构造方法
/// </summary>
/// <param name="items">变长形参</param>
public CStringList(params string[] items)
{
} /// <summary>
/// 在不改变集合当前内容的情况下,使集合容量增加IncLength
/// </summary>
protected void Expend()
{ } /// <summary>
/// 只读属性,返回当前集合中的元素个数
/// </summary>
public int Count
{
get { return 0; } // 假的
} /// <summary>
/// 在集合尾部追加一个元素,若失败抛出Exception
/// </summary>
/// <param name="item">新加入的元素</param>
virtual public void Add(string item)
{ } /// <summary>
/// 在集合尾部追加任意个元素,若失败抛出Exception
/// </summary>
/// <param name="items">变长形参</param>
virtual public void Add(params string[] items)
{ } /// <summary>
/// 在集合指定位置插入一个元素,若失败抛出Exception
/// </summary>
/// <param name="item">被插入的元素</param>
/// <param name="index">插入位置</param>
virtual public void insert(string item, int index)
{ } /// <summary>
/// 移除指定位置的元素,若失败抛出Exception
/// </summary>
/// <param name="Index"></param>
public void RemoveAt(int Index)
{ } /// <summary>
/// 清空集合所有元素
/// </summary>
public void Clear()
{ } /// <summary>
/// 查找指定字符串aString在集合中的位置(大小写敏感)
/// </summary>
/// <param name="item">被查找的字符串</param>
/// <returns>找到则返回对应位置,未找到返回-1</returns>
virtual public int Find(string aString)
{ } /// <summary>
/// 对集合进行排序
/// </summary>
/// <param name="Opt">参数=asc为正向排序,=desc为逆向排序</param>
public void Sort( string Opt)
{ } /// <summary>
/// 只读属性,返回集合中字符串长度最大的一项,若存在多项长度相等返回其中的第一项
/// </summary>
public string MaxLenItem
{
get { return null; }
} /// <summary>
/// 只读属性,返回集合中字符串长度最小的一项,若存在多项长度相等返回其中的第一项
/// </summary>
public string MinLenItem
{
get { return null; }
} /// <summary>
/// 按位置输出集合中的各项元素,输出格式为:
///
/// 集合中共有?个元素,分别为:
/// 元素1
/// 元素2
/// ……
/// 元素n
/// </summary>
public void Show()
{ }
} public class CMyDirectory:CStringList
{
/// <summary>
/// 检查字符串数组参数strArr中是否包含重复数据项(大小写不敏感)
/// </summary>
/// <param name="strArr">被检查的字符串数据</param>
/// <returns>true:不包含重复项;false:包含重复项</returns>
public bool IsValid(string[] strArr)
{ }
/// <summary>
/// 集合尾部添加一个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="item">追加的新元素</param>
public override void Add(string item)
{
}
/// <summary>
/// 集合尾部添加若干个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="items">追加的新元素</param>
public override void Add(params string[] items)
{
} /// <summary>
/// 在指定的位置插入一个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="item">插入的新元素</param>
/// <param name="index">插入位置</param>
public override void insert(string item, int index)
{
} /// <summary>
/// 查找一个指定元素在集合中的位置(大小写不敏感)。
/// </summary>
/// <param name="aString"></param>
/// <returns></returns>
public override int Find(string aString)
{
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
public class CStringList
{
public const int IncLength = 20;
protected string[] list;
protected int count;
/// <summary>
/// 空参构造方法
/// </summary>
public CStringList()
{
} /// <summary>
/// 以任意个(小于20)字符串参数构造对象的构造方法
/// </summary>
/// <param name="items">变长形参</param>
public CStringList(params string[] items)
{
} /// <summary>
/// 在不改变集合当前内容的情况下,使集合容量增加IncLength
/// </summary>
protected void Expend()
{ } /// <summary>
/// 只读属性,返回当前集合中的元素个数
/// </summary>
public int Count
{
get { return 0; } // 假的
} /// <summary>
/// 在集合尾部追加一个元素,若失败抛出Exception
/// </summary>
/// <param name="item">新加入的元素</param>
virtual public void Add(string item)
{ } /// <summary>
/// 在集合尾部追加任意个元素,若失败抛出Exception
/// </summary>
/// <param name="items">变长形参</param>
virtual public void Add(params string[] items)
{ } /// <summary>
/// 在集合指定位置插入一个元素,若失败抛出Exception
/// </summary>
/// <param name="item">被插入的元素</param>
/// <param name="index">插入位置</param>
virtual public void insert(string item, int index)
{ } /// <summary>
/// 移除指定位置的元素,若失败抛出Exception
/// </summary>
/// <param name="Index"></param>
public void RemoveAt(int Index)
{ } /// <summary>
/// 清空集合所有元素
/// </summary>
public void Clear()
{ } /// <summary>
/// 查找指定字符串aString在集合中的位置(大小写敏感)
/// </summary>
/// <param name="item">被查找的字符串</param>
/// <returns>找到则返回对应位置,未找到返回-1</returns>
virtual public int Find(string aString)
{ } /// <summary>
/// 对集合进行排序
/// </summary>
/// <param name="Opt">参数=asc为正向排序,=desc为逆向排序</param>
public void Sort( string Opt)
{ } /// <summary>
/// 只读属性,返回集合中字符串长度最大的一项,若存在多项长度相等返回其中的第一项
/// </summary>
public string MaxLenItem
{
get { return null; }
} /// <summary>
/// 只读属性,返回集合中字符串长度最小的一项,若存在多项长度相等返回其中的第一项
/// </summary>
public string MinLenItem
{
get { return null; }
} /// <summary>
/// 按位置输出集合中的各项元素,输出格式为:
///
/// 集合中共有?个元素,分别为:
/// 元素1
/// 元素2
/// ……
/// 元素n
/// </summary>
public void Show()
{ }
} public class CMyDirectory:CStringList
{
/// <summary>
/// 检查字符串数组参数strArr中是否包含重复数据项(大小写不敏感)
/// </summary>
/// <param name="strArr">被检查的字符串数据</param>
/// <returns>true:不包含重复项;false:包含重复项</returns>
public bool IsValid(string[] strArr)
{ }
/// <summary>
/// 集合尾部添加一个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="item">追加的新元素</param>
public override void Add(string item)
{
}
/// <summary>
/// 集合尾部添加若干个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="items">追加的新元素</param>
public override void Add(params string[] items)
{
} /// <summary>
/// 在指定的位置插入一个新元素,且不能和已有元素重复(大小写不敏感)
/// </summary>
/// <param name="item">插入的新元素</param>
/// <param name="index">插入位置</param>
public override void insert(string item, int index)
{
} /// <summary>
/// 查找一个指定元素在集合中的位置(大小写不敏感)。
/// </summary>
/// <param name="aString"></param>
/// <returns></returns>
public override int Find(string aString)
{
}
}
}
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
{
/// <summary>
/// 数组增值数
/// </summary>
public const int IncLength = 5;
/// <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 > 20)
{
maxLength = 20;
}
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]);
}
}
}
{
/// <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;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static CStringList list = new CStringList();
static CMyDirectory dic = new CMyDirectory();
static void Main(string[] args)
{
list.Add("bB", "aA", "cC");
list.Add("你晕", "我晕", "大家晕");
CheckList(); // 测试长度
C("Count:" + list.Count);
CheckList(); // 数组长度测试,测试时将IncLength设为5。
//list.Add("aA", "bB", "cC");
//CheckList(); // 测试插入
//list.Insert("dD", 0);
//CheckList();
// 测试插入-1
//list.Insert("dD", -1);
//CheckList();
// 测试插入大于count
//list.Insert("dD", 5);
//CheckList(); // 测试删除
//list.RemoveAt(1);
//CheckList();
// 测试删除-1
//list.RemoveAt(-1);
//CheckList();
// 测试删除大于count
//list.RemoveAt(3);
//CheckList(); // 测试删除,然后重新添加
//list.Clear();
//C("Count:" + list.Count);
//list.Add("dD"); // 测试查找大小写敏感字符串
//C(list.Find("AA")); // 测试查找
//C(list.Find("aA")); // 测试排序
//list.Insert("bBb", 1);
//list.Sort();
//CheckList();
//list.Sort("desc"); // 测试MaxLenItem,MinLenItem
//list.Insert("a", 1);
//list.Insert("aaa",2);
//C("MaxLenItem:" + list.MaxLenItem, "MinLenItem:" + list.MinLenItem); //============================以下是测试CMyDirectory=================================== dic.Add("bB", "aA", "cC");
dic.Add("你晕", "我晕", "大家晕");
CheckDic();
// 测试IsValid,大小写不敏感
//C(dic.IsValid(new string[] { "aa", "aA", "bb" }));
//C(dic.IsValid(new string[] { "aa", "c", "bb" }));
//CheckDic(); // 测试Add,大小写不敏感,aa插入无效,dd插入成功
//dic.Add("aa");
//dic.Add("dd");
//CheckDic(); // 测试Add多个,只插入dd,ee,ff,而aa则插入失败
//dic.Add("aa", "dd", "ee","ff");
//CheckDic(); // 测试Insert,大小写敏感,aa插入无效,dd插入成功
//dic.Insert("aa", 2);
//dic.Insert("dd", 2);
//CheckDic(); // 测试Find,大小写敏感,找到aa,找不到dd
//C(dic.Find("aa"));
//C(dic.Find("dd"));
//CheckDic();
Console.ReadKey();
} static void CheckList()
{
Console.WriteLine("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Console.WriteLine("检查数组用:");
// 测试Show
list.Show();
Console.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
} static void CheckDic()
{
Console.WriteLine("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Console.WriteLine("检查数组用:");
// 测试Show
dic.Show();
Console.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
} static void C(params object[] array)
{
string[] s = new string[array.Length];
for (int i = 0; i < s.Length; i++)
{
s[i] = array[i].ToString();
}
Console.WriteLine(string.Join("\n", array));
}
}
}