using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JinYuan.Helper { public class ArrayHelper { /// /// 判断两个数组的元素是否相等 /// /// /// /// public static bool Equal(T[] arr1, T[] arr2) { bool res = false; if (arr1 == null || arr2 == null) { return res; } if (arr1.Length == arr2.Length) { res = true; for (int index = 0; index < arr1.Length; ++index) { if (!arr1[index].Equals(arr2[index])) { res = false; break; } } } return res; } /// /// 获取数组的子数组 /// /// 类型 /// 数组 /// 起始索引 /// 长度 /// public static T[] GetSubArray(T[] arr, int startIndex, int length) { T[] res = new T[length]; for (int index = 0; index < length; ++index) { res[index] = arr[startIndex + index]; } return res; } /// /// 数组合并 /// /// /// /// /// public static byte[] Combine(IEnumerable tList) { int allLength = 0; foreach (byte[] t in tList) { allLength += t.Length; } byte[] res = new byte[allLength]; int lenIndex = 0; foreach (byte[] t in tList) { Array.Copy(t, 0, res, lenIndex, t.Length); lenIndex += t.Length; } return res; } /// /// 数组格式化输出字符串 /// /// 待处理数据 /// public static string ArrayFormatOutput(T[] arr) { StringBuilder res = new StringBuilder(); for (int index = 0; index < arr.Count() - 1; ++index) { res.Append(string.Format("{0}-", arr[index].ToString())); } if (arr.Count() - 1 > 0) { res.Append(arr[arr.Count() - 1]); } return res.ToString(); } /// /// 数组格式化输出字符串 /// /// /// 待处理数据 /// buffer 参数中开始发送数据的位置,该位置从零开始计数。 /// 要发送的字节数。 /// public static string ArrayFormatOutput(T[] arr, int offset, int size) { StringBuilder res = new StringBuilder(); for (int index = offset; index < size; ++index) { res.Append(string.Format("{0}-", arr[index].ToString())); } if (size - 1 > 0) { res.Append(arr[size]); } return res.ToString(); } } }