.NET Core(C#)数组Array相关的工具类方法及示例代码
时间:2022-08-05
1、设置数组默认值方法
public static void Fill(byte[] array,byte defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public static void Fill(char[] array, char defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public static void Fill<T>(T[] array, T defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public static void Fill(byte[] a, int fromIndex, int toIndex, byte val) { RangeCheck(a.Length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++) a[i] = val; } private static void RangeCheck(int length, int fromIndex, int toIndex) { if (fromIndex > toIndex) { throw new ArgumentException( "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); } if (fromIndex < 0) { throw new IndexOutOfRangeException("fromIndex(" + fromIndex + ")"); } if (toIndex > length) { throw new IndexOutOfRangeException( "toIndex(" + toIndex + ")"); } } public static void Fill(int[] array, byte defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } }
2、Array 转成ArrayList
public static ArrayList AsList(Array arr) { if (arr.Length <= 0) return new ArrayList(); ArrayList al = new ArrayList(arr.Length); for (int i = 0; i < arr.Length; i++) { al.Add(arr.GetValue(i)); } return al; }
3、比较两个数组是否相等
public new static bool Equals(object a1, object b1) { if (a1 == null || b1 == null) return false; Array a = a1 as Array; Array b = b1 as Array; if (a.Length != b.Length) return false; for (int i = 0; i < a.Length; i++) { if (!a.GetValue(i).Equals(b.GetValue(i))) return false; } return true; } public static bool Equals(Object[] a, Object[] a2) { if (a == a2) return true; if (a == null || a2 == null) return false; int length = a.Length; if (a2.Length != length) return false; for (int i = 0; i < length; i++) { Object o1 = a[i]; Object o2 = a2[i]; if (!(o1 == null ? o2 == null : o1.Equals(o2))) return false; } return true; } public static bool DeepEquals(Object[] a1, Object[] a2) { if (a1 == a2) return true; if (a1 == null || a2==null) return false; int length = a1.Length; if (a2.Length != length) return false; for (int i = 0; i < length; i++) { Object e1 = a1[i]; Object e2 = a2[i]; if (e1 == e2) continue; if (e1 == null) return false; // Figure out whether the two elements are equal bool eq = DeepEquals0(e1, e2); if (!eq) return false; } return true; } static bool DeepEquals0(Object e1, Object e2) { bool eq; if (e1 is Object[] && e2 is Object[]) eq = DeepEquals ((Object[]) e1, (Object[]) e2); else if (e1 is byte[] && e2 is byte[]) eq = Equals((byte[]) e1, (byte[]) e2); else if (e1 is short[] && e2 is short[]) eq = Equals((short[]) e1, (short[]) e2); else if (e1 is int[] && e2 is int[]) eq = Equals((int[]) e1, (int[]) e2); else if (e1 is long[] && e2 is long[]) eq = Equals((long[]) e1, (long[]) e2); else if (e1 is char[] && e2 is char[]) eq = Equals((char[]) e1, (char[]) e2); else if (e1 is float[] && e2 is float[]) eq = Equals((float[]) e1, (float[]) e2); else if (e1 is double[] && e2 is double[]) eq = Equals((double[]) e1, (double[]) e2); else if (e1 is bool[] && e2 is bool[]) eq = Equals((bool[]) e1, (bool[]) e2); else eq = e1.Equals(e2); return eq; }
4、数组的拷贝方法
public static void ArrayMoveWithin(Object[] array, int moveFrom, int moveTo, int numToMove) { // If we're not asked to do anything, return now if (numToMove <= 0) { return; } if (moveFrom == moveTo) { return; } // Check that the values supplied are valid if (moveFrom < 0 || moveFrom >= array.Length) { throw new ArgumentException("The moveFrom must be a valid array index"); } if (moveTo < 0 || moveTo >= array.Length) { throw new ArgumentException("The moveTo must be a valid array index"); } if (moveFrom + numToMove > array.Length) { throw new ArgumentException("Asked to move more entries than the array has"); } if (moveTo + numToMove > array.Length) { throw new ArgumentException("Asked to move to a position that doesn't have enough space"); } // Grab the bit to move Object[] toMove = new Object[numToMove]; Array.Copy(array, moveFrom, toMove, 0, numToMove); // Grab the bit to be shifted Object[] toShift; int shiftTo; if (moveFrom > moveTo) { // Moving to an earlier point in the array // Grab everything between the two points toShift = new Object[(moveFrom - moveTo)]; Array.Copy(array, moveTo, toShift, 0, toShift.Length); shiftTo = moveTo + numToMove; } else { // Moving to a later point in the array // Grab everything from after the toMove block, to the new point toShift = new Object[(moveTo - moveFrom)]; Array.Copy(array, moveFrom + numToMove, toShift, 0, toShift.Length); shiftTo = moveFrom; } // Copy the moved block to its new location Array.Copy(toMove, 0, array, moveTo, toMove.Length); // And copy the shifted block to the shifted location Array.Copy(toShift, 0, array, shiftTo, toShift.Length); // We're done - array will now have everything moved as required } public static byte[] CopyOf(byte[] source, int newLength) { byte[] result = new byte[newLength]; Array.Copy(source, 0, result, 0, Math.Min(source.Length, newLength)); return result; } internal static int[] CopyOfRange(int[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new ArgumentException(from + " > " + to); int[] copy = new int[newLength]; Array.Copy(original, from, copy, 0, Math.Min(original.Length - from, newLength)); return copy; } internal static byte[] CopyOfRange(byte[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new ArgumentException(from + " > " + to); byte[] copy = new byte[newLength]; Array.Copy(original, from, copy, 0, Math.Min(original.Length - from, newLength)); return copy; }
5、工具类方法完整代码
using System; using System.Collections; using System.Text; using System.Collections.Generic; using NPOI.Util.Collections; namespace CJAVAPY.Util { public >Arrays { public static void Fill(byte[] array,byte defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public static void Fill(char[] array, char defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public static void Fill<T>(T[] array, T defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public static void Fill(byte[] a, int fromIndex, int toIndex, byte val) { RangeCheck(a.Length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++) a[i] = val; } private static void RangeCheck(int length, int fromIndex, int toIndex) { if (fromIndex > toIndex) { throw new ArgumentException( "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); } if (fromIndex < 0) { throw new IndexOutOfRangeException("fromIndex(" + fromIndex + ")"); } if (toIndex > length) { throw new IndexOutOfRangeException( "toIndex(" + toIndex + ")"); } } public static ArrayList AsList(Array arr) { if (arr.Length <= 0) return new ArrayList(); ArrayList al = new ArrayList(arr.Length); for (int i = 0; i < arr.Length; i++) { al.Add(arr.GetValue(i)); } return al; } public static void Fill(int[] array, byte defaultValue) { for (int i = 0; i < array.Length; i++) { array[i] = defaultValue; } } public new static bool Equals(object a1, object b1) { if (a1 == null || b1 == null) return false; Array a = a1 as Array; Array b = b1 as Array; if (a.Length != b.Length) return false; for (int i = 0; i < a.Length; i++) { if (!a.GetValue(i).Equals(b.GetValue(i))) return false; } return true; } public static bool Equals(Object[] a, Object[] a2) { if (a == a2) return true; if (a == null || a2 == null) return false; int length = a.Length; if (a2.Length != length) return false; for (int i = 0; i < length; i++) { Object o1 = a[i]; Object o2 = a2[i]; if (!(o1 == null ? o2 == null : o1.Equals(o2))) return false; } return true; } public static void ArrayMoveWithin(Object[] array, int moveFrom, int moveTo, int numToMove) { // If we're not asked to do anything, return now if (numToMove <= 0) { return; } if (moveFrom == moveTo) { return; } // Check that the values supplied are valid if (moveFrom < 0 || moveFrom >= array.Length) { throw new ArgumentException("The moveFrom must be a valid array index"); } if (moveTo < 0 || moveTo >= array.Length) { throw new ArgumentException("The moveTo must be a valid array index"); } if (moveFrom + numToMove > array.Length) { throw new ArgumentException("Asked to move more entries than the array has"); } if (moveTo + numToMove > array.Length) { throw new ArgumentException("Asked to move to a position that doesn't have enough space"); } // Grab the bit to move Object[] toMove = new Object[numToMove]; Array.Copy(array, moveFrom, toMove, 0, numToMove); // Grab the bit to be shifted Object[] toShift特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。