Re-adjustment array length
In the .NET in the array length, the length is not adjustable, in fact, just create an array with the Array's static method createInstance, and then put the data
Copy.
Let's take a look at the one-dimensional array (as an example of INT):
Public Class APP
{
[Stathread]
Static void main (string [] args)
{
Int [] arr = new int [] {1, 2, 3};
PrintVALUES (ARR);
Arr = (int []) Redim (Arr, 5);
PrintVALUES (ARR);
Arr = (int []) Redim (Arr, 4);
PrintVALUES (ARR);
}
// Adjust the length
Public Static Array Redim (Array OrigaRray, Int Length)
{
/ / Determine the type of each element
TYPE T = OrigArray.gettype (). GetElementType ();
// Create a new array
Array NEWARRAY = Array.createInstance (T, Length));
/ / The data in the original number group is copied to the new array
Array.copy (OrigaRray, 0, Newarray, 0, Math.min (OrigaRray.Length, Length));
Return Newarray;
}
//Output Data
Public Static Void PrintValues (Array Myarr)
{
System.collections.ienumerator myenumerator = myarr.getenumerator ();
INT i = 0;
INT cols = myarr.getlength (Myarr.rank - 1);
While (myenumerator.movenext ())
{
IF (i { i ; } Else { Console.writeLine (); i = 1; } Console.write ("/ t {0}", myenumerator.current); } Console.writeLine (); } } The output is: 1, 2, 3 1, 2, 3, 0, 0 1, 2, 3, 0 The above is a one-dimensional array, if it is a two-dimensional array or a multi-dimensional number? Add a column behind the two-dimensional array; Public Class APP { [Stathread] Static void main (string [] args) { // Create an INT type 2D number of groups Int [,] my2darray = (int [,]) Array.createInstance (TypeOf (int), 2, 3); For (int i = my2darray.getLowerBound (0); i <= my2darray.getupperbound (0); i ) For (int J = my2darray.getLowerbound (1); j <= my2darray.getupperbound (1); J ) My2darray.setValue (i J, I, J); PrintValues (My2daRray); Console.writeLine (); My2daRray = (int []) Redim (My2Darray, 2, 4); PrintValues; My2darray = (int [,]) Redim (My2Darray, 2, 2); PrintValues (My2daRray); } // Adjust the length Public Static Array Redim (Array OrigArray, Params Int " { / / Determine the type of each element TYPE T = OrigArray.gettype (). GetElementType (); // Create a new array Array Newarray = Array.createInstance (T, Length))))) / / The data in the original number group is copied to the new array For (int i = OrigaRray.getLowerBound (0); i <= math.min (OrigaRray.getupperbound (0), NEWARRAY.GetupperBound (0)); i ) For (int J = OrigArray.getLowerBound (1); j <= math.min (OrigaRray.getupperBound (1), NEWARRAY.GetupperBound (1)); J ) NEWARRAY.SetValue (OrigaRray.getValue (I, J), I, J); / / There is no copy method here. If this method is used, all the data in the original number is copied to the new array. Return Newarray; } //Output Data Public Static Void PrintValues (Array Myarr) { System.collections.ienumerator myenumerator = myarr.getenumerator (); INT i = 0; INT cols = myarr.getlength (Myarr.rank - 1); While (myenumerator.movenext ()) { IF (i { i ; } Else { Console.writeLine (); i = 1; } Console.write ("/ t {0}", myenumerator.current); } Console.writeLine (); } } The output result is: 0, 1, 2 1, 2, 3 0, 1, 2, 0, 0 1, 2, 3, 0, 0 0,1 1, 2 If you use a COPY method in the redim method Slightly change the Redim method Public Static Array Redim (Array OrigArray, Params Int " { INT length = 1; For (int i = 0; i Length * = lengths [i]; TYPE T = OrigArray.gettype (). GetElementType (); Array newArray = array.createinstance (t, lengths); Array.copy (OrigaRray, 0, Newarray, 0, Math.min (OrigaRray.Length, Length); Return Newarray; } The output result is: 0, 1, 2 1, 2, 3 0, 1, 2, 12, 3, 0, 0 0,1 2, 1 Obviously this result is not what we want. If it is three-dimensional, multi-dimensional? Refer to 2D.