1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Runtime.Serialization;5 usingSystem.Runtime.Serialization.Formatters.Binary;6 7 namespaceConsoleApplication38 {9 class ProgramTen { One Static voidMain (string[] args) A { - //the source array to manipulate -list<int> Originalarray =Newlist<int>(); the for(inti =1; I <=Ten; i++) - { - Originalarray.add (i); - } +Console.Write ("Source array:"); - foreach(intIteminchOriginalarray) + { AConsole.Write (item +" "); at } - Console.WriteLine (); - //gets a sub-array of the specified length -list<list<int>> resultarray = Subchildarray (Originalarray, Originalarray.count-1); - - for(inti =0; i < Resultarray.count; i++) in { -Console.Write ("Sub-array"+ (i +1) +":"); to for(intj =0; J < Resultarray[i]. Count; J + +) + { -Console.Write (Resultarray[i][j] +" "); the } * Console.WriteLine (); $ }Panax Notoginseng -Console.WriteLine ("number of sub-arrays:"+resultarray.count); the Console.readkey (); + } A /// <summary> the ///gets a sub-array of the specified length + /// </summary> - /// <param name= "Originalarray" >Source Array</param> $ /// <param name= "Arraylength" >to remove the length of the sub-array</param> $ /// <returns></returns> - Staticlist<list<int>> Subchildarray (list<int> Originalarray,intarraylength) - { thelist<list<int>> result =Newlist<list<int>>(); -list<int> one =Newlist<int>();Wuyi the for(inti =0; i < Originalarray.count; i++) - { Wu One . ADD (Originalarray[i]); -ADD (Originalarray.getrange (i +1, Originalarray.count-1-i), arraylength-1, result, one); About if(One. Count! =0) $One. RemoveAt (one. Count-1); - //It's a good place to use one. Clear (); - //Once the cycle is complete, you can clear one and then start over again - } A returnresult; + } the /// <summary> - ///functions for recursion $ /// </summary> the /// <param name= "Array" ></param> the /// <param name= "n" ></param> the /// <param name= "result" ></param> the /// <param name= "one" ></param> - Static voidADD (list<int> Array,intN, list<list<int>> result, list<int>One ) in { the //if n=0, it indicates that the Count property of one is equal to n the if(n = =0) About { theResult. ADD (Clone (one) aslist<int>); the //before the next one, remove the last element theOne. RemoveAt (one. Count-1); + } - Else the {Bayi for(inti =0; I < array. Count; i++) the { the One . ADD (Array[i]); - //recursion here, while N-1 -ADD (Array. GetRange (i +1, array. Count-1-i), N-1, result, one); the } the //after the end of the round, remove the last element theOne. RemoveAt (one. Count-1); the } - } the /// <summary> the ///functions for copying objects the /// </summary>94 /// <param name= "obj" ></param> the /// <returns></returns> the Public Static ObjectClone (Objectobj) the {98 using(MemoryStream ms =NewMemoryStream ()) About { -IFormatter Formattor =NewBinaryFormatter ();101 Formattor. Serialize (MS, obj);102Ms. Seek (0, seekorigin.begin);103 returnFormattor. Deserialize (MS);104 } the }106 }107}
Transferred from: http://www.cnblogs.com/xiangism/archive/2009/09/06/1561398.html
Returns a specified length of sub-array from the array [go]