class Program
{
static void Main(string[] args)
{
SortHelper<int> isorter = new SortHelper<int>();
int[] iarray = {8, 7, 1, 2, 12};
isorter.BubbleSort(iarray);
foreach (int item in iarray)
{
Console.Write(item+ ", ");
}
Console.ReadKey();
}
}
public class SortHelper<T> where T : IComparable
{
public void BubbleSort(T[] array)
{
int length = array.Length;
for (int i = 0; i <= length -2; i++)
{
for (int j = length - 1; j >= 1; j--)
{
if (array[j].CompareTo(array[j-1]) < 0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
Book[] bookArray = new Book[2];
Book book1 = new Book(100, "书一");
Book book2 = new Book(80, "书二");
bookArray[0] = book1;
bookArray[1] = book2;
Console.WriteLine("冒泡之前:");
foreach (Book b in bookArray)
{
Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
}
SortHelper<Book> sorter = new SortHelper<Book>();
sorter.BubbleSort(bookArray);
Console.WriteLine("冒泡之后:");
foreach (Book b in bookArray)
{
Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
}
Console.ReadKey();
}
}
public class SortHelper<T> where T : IComparable
{
public void BubbleSort(T[] array)
{
int length = array.Length;
for (int i = 0; i <= length -2; i++)
{
for (int j = length - 1; j >= 1; j--)
{
if (array[j].CompareTo(array[j-1]) < 0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
}
//自定义类实现IComparable接口
public class Book : IComparable
{
private int price;
private string title;
public Book(){}
public Book(int price, string title)
{
this.price = price;
this.title = title;
}
public int Price
{
get { return this.price; }
}
public string Title
{
get { return this.title; }
}
public int CompareTo(object obj)
{
Book book = (Book)obj;
return this.Price.CompareTo(book.Price);
}
}
//方法泛型
public class MethodSortHelper
{
public void BubbleSort<T>(T[] array) where T : IComparable
{
int length = array.Length;
for (int i = 0; i <= length - 2; i++)
{
for (int j = length - 1; j >= 1; j--)
{
if (array[j].CompareTo(array[j - 1]) < 0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
Book[] bookArray = new Book[2];
Book book1 = new Book(100, "书一");
Book book2 = new Book(80, "书二");
bookArray[0] = book1;
bookArray[1] = book2;
Console.WriteLine("冒泡之前:");
foreach (Book b in bookArray)
{
Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
}
MethodSortHelper sorter = new MethodSortHelper();
sorter.BubbleSort<Book>(bookArray);
Console.WriteLine("冒泡之后:");
foreach (Book b in bookArray)
{
Console.WriteLine("书名:{0},价格:{1}", b.Title, b.Price);
}
Console.ReadKey();
}
}
MethodSortHelper sorter = new MethodSortHelper(); sorter.BubbleSort<Book>(bookArray);
MethodSortHelper sorter = new MethodSortHelper();
sorter.BubbleSort(bookArray);
ArrayList list = new ArrayList();
for(int i = 0; i < 3; i++)
{
list.Add(i); //Add接收的参数类型是引用类型object,这里包含了隐式装箱
}
for(int i = 0; i < 3; i++)
{
int value = (int)list[i]; //引用类型强转成值类型,拆箱
Console.WriteLine(value);
}
List<int> list = new List<int>();
for(int i = 0; i < 3; i++)
{
list.Add(i);
}
for(int i = 0; i < 3; i++)
{
int value = list[i];
Console.WriteLine(value);
}
ArrayList list = new ArrayList(); int i = 100; list.Add(i); string value = (string)list[0];
List<int> list = new List<int>(); int i = 100; list.Add(i); string value = (string)list[0];
using IntList = List<int>; IntList list = new IntList(); list.Add(1);
public class IntList : List<int>{}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有