Array.Copy 一种浅拷贝
int[] intArray = new int[5] { 1, 2, 3, 4, 5 };
Object[] objArray = new Object[5] {26,27,28,29,30 };
//Array.Copy(intArray, objArray, 2);//将intArray的前2个元素拷贝覆盖到ObjArray的前两个元素
Array.Copy(objArray, objArray.GetUpperBound(0) - 1, intArray, intArray.GetUpperBound (0)-1,2);//GetUpperBound (0)是取数组的一维大小
Array.Sort 快速排序机制,默认为升序
int[] numbers = new int[10];
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
numbers[i] = rnd.Next(100);//我们用随机数来增加练习的有趣性
}
Array.Sort(numbers);
假如需要得到的数组为逆序的,添加如下代码:
class IntComparer : IComparer<int>
{
#region IComparer<int> 成员
public int Compare(int x, int y)
{
if (_type == "DESC")
return y.CompareTo(x);//我们这里正好让它反过来,就实现了降序了
else
return x.CompareTo(y);
}
#endregion
private string _type = "ASC";
public IntComparer(string type)
{
_type = type;
}
}
调用:Array.Sort(intArray, new IntComparer("DESC"));