C# 一个自己写的树结构代码(2)-Array,HashTable,List,String数据结构操作封装
2016-10-20 11:32
766 查看
Array,HashTable,List,String数据结构操作封装。仅仅实现了功能,没有考虑性能。如果有更好的实现方式,希望能互相交流。
public class ArrayUtils
{
static public T Get<T>(T[] arr, int n, T Default)
{
if (arr == null) return Default;
if (n < 0) return Default;
T value = Default;
if (arr.Count() > n)
try
{
value = arr
;
if (value == null) value = Default;
}
catch
{
}
return value;
}
/// <summary>
/// 从数组中截取一部分成新的数组
/// </summary>
/// <param name="Source">原数组</param>
/// <param name="StartIndex">原数组的起始位置</param>
/// <param name="EndIndex">原数组的截止位置</param>
/// <returns></returns>
public static T[] SplitArray<T>(T[] Source, int StartIndex, int EndIndex)
{
if (Source.Length <= StartIndex)
{
return new T[0];
}
if (StartIndex < 0) StartIndex = 0;
if (EndIndex > Source.Length - 1) EndIndex = Source.Length - 1;
try
{
T[] result = new T[EndIndex - StartIndex + 1];
for (int i = 0; i <= EndIndex - StartIndex; i++) result[i] = Get<T>(Source, i + StartIndex, default(T));
return result;
}
catch (IndexOutOfRangeException ex)
{
Common.WriteException(ex);
return new T[0];
}
catch (Exception ex)
{
Common.WriteException(ex);
return new T[0];
}
}
/// <summary>
/// check string is null or empty.
/// </summary>
/// <param name="Arr"></param>
/// <returns></returns>
public static bool IsNullOrEmpty<T>(T[] Arr)
{
return Arr == null || Arr.Length == 0;
}
/// <summary>
/// check if arr index is in arr
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Arr"></param>
/// <param name="n"></param>
/// <returns></returns>
public static bool IsValidIndex<T>(T[] Arr, int n)
{
if (IsNullOrEmpty(Arr)) return false;
return n >= 0 && n < Arr.Length - 1;
}
public static bool IsBetweenIndex<T>(T[] Arr, int n, int nStartIndex, int nEndIndex)
{
if ((IsValidIndex<T>(Arr, nStartIndex) && n < nStartIndex) ||
(IsValidIndex<T>(Arr, nEndIndex) && n > nEndIndex))
{
return false;
}
return true;
}
}
public class ArrayUtils
{
static public T Get<T>(T[] arr, int n, T Default)
{
if (arr == null) return Default;
if (n < 0) return Default;
T value = Default;
if (arr.Count() > n)
try
{
value = arr
;
if (value == null) value = Default;
}
catch
{
}
return value;
}
/// <summary>
/// 从数组中截取一部分成新的数组
/// </summary>
/// <param name="Source">原数组</param>
/// <param name="StartIndex">原数组的起始位置</param>
/// <param name="EndIndex">原数组的截止位置</param>
/// <returns></returns>
public static T[] SplitArray<T>(T[] Source, int StartIndex, int EndIndex)
{
if (Source.Length <= StartIndex)
{
return new T[0];
}
if (StartIndex < 0) StartIndex = 0;
if (EndIndex > Source.Length - 1) EndIndex = Source.Length - 1;
try
{
T[] result = new T[EndIndex - StartIndex + 1];
for (int i = 0; i <= EndIndex - StartIndex; i++) result[i] = Get<T>(Source, i + StartIndex, default(T));
return result;
}
catch (IndexOutOfRangeException ex)
{
Common.WriteException(ex);
return new T[0];
}
catch (Exception ex)
{
Common.WriteException(ex);
return new T[0];
}
}
/// <summary>
/// check string is null or empty.
/// </summary>
/// <param name="Arr"></param>
/// <returns></returns>
public static bool IsNullOrEmpty<T>(T[] Arr)
{
return Arr == null || Arr.Length == 0;
}
/// <summary>
/// check if arr index is in arr
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Arr"></param>
/// <param name="n"></param>
/// <returns></returns>
public static bool IsValidIndex<T>(T[] Arr, int n)
{
if (IsNullOrEmpty(Arr)) return false;
return n >= 0 && n < Arr.Length - 1;
}
public static bool IsBetweenIndex<T>(T[] Arr, int n, int nStartIndex, int nEndIndex)
{
if ((IsValidIndex<T>(Arr, nStartIndex) && n < nStartIndex) ||
(IsValidIndex<T>(Arr, nEndIndex) && n > nEndIndex))
{
return false;
}
return true;
}
}
public class HashTableUtils { static public bool IsNullOrEmpty(Hashtable ht) { if (ht == null) return true; if (ht.Count == 0) return true; return false; } static public bool Equal(Hashtable ht1, Hashtable ht2) { if (ht1 == null || ht2 == null) return false; if (ht1.Count != ht2.Count) return false; foreach (DictionaryEntry item in ht1) { object obj = GetValue<object>(ht2, item.Key.ToString(), null); if (!obj.Equals(item.Value)) return false; } return true; } static public int Add(ref Hashtable ht, object key, object value) { if (ht == null) ht = new Hashtable(); if (key != null && value != null) { if (!ht.ContainsKey(key)) ht.Add(key, value); } return 0; } static public string GetString(Hashtable ht, string key, string Default = "") { object obj = GetValue(ht, key, Default); return obj == null && Default == null ? null : obj.ToString(); } static public T GetValue<T>(Hashtable ht, string key, T Default) { T value = Default; if (ht == null || ht.Count == 0) return value; try { value = (T)ht[key]; if (value == null) value = Default; } catch { } return value; } static public int SetValue(ref Hashtable ht, object key, object value) { int nRet = 0; if (ht == null) ht = new Hashtable(); if (key != null) { if (!ht.ContainsKey(key)) { ht.Add(key, value); } else { ht[key] = value; nRet = -1; } } return nRet; } }
public class ListUtils { static public bool IsNullOrEmpty<T>(List<T> list) { if (list == null) return true; if (list.Count == 0) return true; return false; } static public T Get<T>(List<T> list, int n, T Default) { T value = Default; if (IsNullOrEmpty<T>(list)) return value; if (list.Count > n && n >= 0) try { value = list ; if (value == null) value = Default; } catch { } return value; } static public T Pop<T>(ref List<T> list, T Default = default(T)) { T res = Get<T>(list, 0, Default); Remove<T>(ref list, 0); return res; } public static int Remove<T>(ref List<T> list, int n) { if (IsNullOrEmpty(list)) return -1; if (n < 0 || n > list.Count - 1) return -1; list.RemoveAt(n); return 0; } }
public class StrUtils { public static bool Exist(string str, string tar, bool IgnoreCase = false) { if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(tar)) return false; if (IgnoreCase) { if (str.ToUpper().Length > 0 && tar.ToUpper().Length > 0) { str = str.ToUpper(); tar = tar.ToUpper(); } } int pos = str.IndexOf(tar, 0); if (pos >= 0) return true; return false; } static public string ConDirs(string root, string dir) { if (!IsExistVisibleChar(root)) { // 说明:not need to connect. if no root // 修改日期:2015-3-5 12:28:39 root = ""; return dir; } if (!IsExistVisibleChar(dir)) dir = ""; bool IsRootDir = IsDirString(root); bool IsDirBgn = IsDirStringBgn(dir); if (!IsRootDir && !IsDirBgn) return root.Trim() + "\\" + dir.Trim(); else if (IsRootDir && IsDirBgn) return Left(root, root.Length - 1).Trim() + "\\" + Mid(dir, 1, dir.Length - 1).Trim(); else return root + dir; } public static string Mid(string str, int nSt 9fe3 artIndex, int nlength) { if (str == null || nlength < 0) return ""; nStartIndex = nStartIndex < 0 ? 0 : nStartIndex; if (str.Length <= Math.Abs(nStartIndex + nlength)) { nlength = str.Length - nStartIndex; } if (nlength <= 0) return ""; return Substring(str, nStartIndex, nlength); } static public bool IsDirStringBgn(string text) { if (text.Length < 1) return false; string lst = Substring(text, 0, 1); return lst == "\\" || lst == "/"; } public static bool IsExistVisibleChar(string str) { if (string.IsNullOrEmpty(str)) return false; Regex VisibleChar = new Regex(@"[^\r\n\t\s\v]+"); return VisibleChar.IsMatch(str); } static public string GetDirPart(string text) { if (!IsExistVisibleChar(text)) text = ""; text = GetSDirString(text); int lstInt = text.LastIndexOf("\\"); int lstInt2 = text.LastIndexOf("/"); if (lstInt < 0 && lstInt2 < 0) { // 说明:error logic return text.now return empty string. // 修改日期:2015-9-1 21:56:05 //Logs.WriteError("path error: "+text); return ""; } lstInt = lstInt > lstInt2 ? lstInt : lstInt2; return Substring(text, 0, lstInt + 1); } static public string GetFilePart(string text) { if (!IsExistVisibleChar(text)) text = ""; text = text.Trim(); int lstInt1 = text.LastIndexOf("\\"); int lstInt2 = text.LastIndexOf("/"); int lstInt = lstInt1 > lstInt2 ? lstInt1 : lstInt2; if (lstInt < 0) return text; return Substring(text, lstInt + 1, text.Length - lstInt - 1); } /// <summary> /// string array trans to string split with flg. /// 2016-02-18 13:24:47 /// add startindex and endindex. /// </summary> /// <param name="arr"></param> /// <param name="split"></param> /// <param name="surround1"></param> /// <param name="surround2"></param> /// <param name="nStartIndex"></param> /// <param name="nEndIndex"></param> /// <returns></returns> public static string FromArr(string[] arr, string split = ";", string surround1 = "", string surround2 = "", int nStartIndex = -1, int nEndIndex = -1) { string str = ""; if (arr == null) return str; int nIndex = -1; foreach (string item in arr) { nIndex++; if (!ArrayUtils.IsBetweenIndex<string>(arr, nIndex, nStartIndex, nEndIndex)) { continue; } if (str.Length > 0) str += split; str += surround1 + item + surround2; } return str; } public static string[] Split(string str, string splitflg) { if (string.IsNullOrEmpty(str)) return new string[] { }; if (string.IsNullOrEmpty(splitflg)) { return new string[] { str }; } string[] strArr = null; RegexOptions ro = RegexOptions.Multiline | RegexOptions.ExplicitCapture; strArr = Regex.Split(str, splitflg, ro); //char[] splitflgArr = splitflg.ToArray(); //strArr = str.Split(splitflgArr); return strArr; } public static string FormatPath(string srcFilePath, bool IsWindows = true) { string splitflg1 = "/", splitflg2 = "\\"; if (string.IsNullOrEmpty(srcFilePath)) return ""; srcFilePath = srcFilePath.Trim(); if (!IsWindows) { splitflg1 = "\\"; splitflg2 = "/"; } srcFilePath.Replace(splitflg1, splitflg2); return srcFilePath; } static public string GetSDirString(string text) { if (text.Length < 1) return text; if (IsDirString(text)) text = Left(text, text.Length - 1); return text; } static public bool IsDirString(string text) { if (text.Length < 1) return false; string lst = Substring(text, text.Length - 1, 1); return lst == "\\" || lst == "/"; } public static string Substring(string str, int nPos, int nlength = -1) { if (string.IsNullOrEmpty(str)) return ""; if (nlength == -1) nlength = str.Length - nPos; char[] ch = new char[nlength]; for (int i = 0; i < nlength; i++) { ch[i] = str[nPos + i]; } return new string(ch); } public static string Left(string str, int nlength) { if (str == null || nlength < 0) return ""; if (str.Length <= Math.Abs(nlength)) return str; return Substring(str, 0, nlength); } }
相关文章推荐
- C# 一个自己写的树结构代码(1)
- c# Array或List有个很实用的ForEach方法,可以直接传入一个方法对集合中元素操作
- C#基础-058 List集合、HashSet集合、HashTable集合、StringBuilder与String运行效率比较
- 【redis,1】java操作redis: 将string、list、map、自己定义的对象保存到redis中
- C#新手入门代码 将一个string字符串转换成char数组
- 一个基于 EasyUI 的前台架构(3)封装操作Tabs的JS代码
- c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出
- 在C#代码中应用Log4Net(五)将Log4Net正确地封装在自己的类库中并进行调用
- C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue
- Linux下用OTL操作MySql(包括自己封装的类库及示例代码下载)
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- C#集合类(Array、Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- 本来从动态壁纸预览页面设置一个动态壁纸回到桌面便可以看到桌面动态壁纸,可以观察得到自己的动态壁纸是否设置成功了(必须知道设置是否成功的结构,因为还有一些操作需要完成)! 但是现在是要在自己的应用中进入
- C#编写一个自己输入内容的代码
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- 粗谈C#里 dictionary,HashTable,List<T>,Array 的性能优缺和实现原理。
- 自己封装的C#操作redis公共类
- Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转
- C# j基本操作-拷贝文件夹的所有内容到另一个文件夹内: 复制代码 1 public static void CopyDir(string srcPath, string实现文件夹的复制以及删除