//泛型类
class Item<T>
{
T[] item;
//构造函数
public Item(T[] obj)
{
item = new T[obj.Length];
for (int i = 0; i < obj.Length; i++)
{
item[i] = obj[i];
}
}
public Type ShowType() { return typeof(T); } //返回类型
public T[] GetItems() { return item; } //返回原数组
//返回打乱顺序后的数组
public T[] GetDisruptedItems()
{
//生成一个新数组:用于在之上计算和返回
T[] temp;
temp = new T[item.Length];
for (int i = 0; i < temp.Length; i++) { temp[i] = item[i]; }
//打乱数组中元素顺序
Random rand = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < temp.Length; i++)
{
int x, y; T t;
x = rand.Next(0, temp.Length);
do
{
y = rand.Next(0, temp.Length);
} while (y == x);
t = temp[x];
temp[x] = temp[y];
temp[y] = t;
}
return temp;
}
}
static void Main(string[] args)
{
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//输出数组类型
Item<int> disrupter = new Item<int>(array);
Console.WriteLine("数组类型:" + disrupter.ShowType().ToString());
//输出数组
Console.Write("原输入数组为:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i].ToString() + ' ');
}
Console.WriteLine();
//输出一个打乱后数组
int[] disruptedArray = disrupter.GetDisruptedItems();
Console.Write("打乱后数组为:");
for (int i = 0; i < disruptedArray.Length; i++)
{
Console.Write(disruptedArray[i].ToString() + ' ');
}
Console.WriteLine();
Console.ReadLine();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有