C#阵列Array排序

五一假期回来,练习一下C#的一些知识,了解一下排序。ide

练习数据:this

int[] ints = { 16, 75, 1, 39, 22, 43, 3, 68, 55 };


写一个类:spa

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplicationDemo { public class Bw { public int[] ArrayData { get; set; } public Bw() { } public Bw(int[] myArrayData) { this.ArrayData = myArrayData; } } }
Source Code

 

为这个类,添加一个方法,arrayToArrayListWithForeach() 便是使用foreach方法,把array数据copy to ArrayList数据集:3d

 

System.Collections.ArrayList _al = new System.Collections.ArrayList(); public void arrayToArrayListWithForeach() { foreach (int i in ArrayData) { _al.Add(i); } }
Source Code

 

把Array数据copy to ArrayList,还能够使用另外的方法,arrayToArrayListWithAddRange()code



public void arrayToArrayListWithAddRange() { _al.AddRange(ArrayData); }
Source Code

 

为上面的类,写一个ArrayList数据集Sort();blog

 

public void Sort() { _al.Sort(); }
Source Code

 

再为类写一个方法,就是输出ArrayList的数据:排序

 

public void Output() { foreach (int i in _al) { Console.WriteLine(i.ToString()); } }
Source Code

 

所须要的方法,均写完,在控制台程序使用它们了。get

 

 

上面#17,#18行代码,能够在类new时,一块儿传入:it

上面#20行代码,因为咱们在Bw这个类别中,有写了另一个方法,因此,也能够这样子:io

OK,实现对数据进行排序:

相关文章
相关标签/搜索