ylbtech-Unitity-CS:Generics
1.A,效果图返回顶部
Unsorted List:
Raul:35
Alessandro:30
Maria:72
Hiroyuki:108
Alok:9
Gunnar:18
Sandra:23
Li:28
Bill:19
Franscoise:45 Sorted List:
Alok:9
Gunnar:18
Bill:19
Sandra:23
Li:28
Alessandro:30
Raul:35
Franscoise:45
Maria:72
Hiroyuki:108
Done
请按任意键继续. . .
1.B,源代码返回顶部
1.B.1,

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text; namespace Generics_CSharp
{
// 尖括号中的类型参数 T。
public class MyList<T> : IEnumerable<T>
{
protected Node head;
protected Node current = null; // 嵌套类型也是 T 上的泛型
protected class Node
{
public Node next;
// T 作为私有成员数据类型。
private T data;
// 在非泛型构造函数中使用的 T。
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
// T 作为属性的返回类型。
public T Data
{
get { return data; }
set { data = value; }
}
} public MyList()
{
head = null;
} // T 作为方法参数类型。
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
} // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
// foreach 迭代。请注意,在 C# 2.0 中,
// 不需要实现 Current 和 MoveNext。
// 编译器将创建实现 IEnumerator<T> 的类。
public IEnumerator<T> GetEnumerator()
{
Node current = head; while (current != null)
{
yield return current.Data;
current = current.Next;
}
} // 必须实现此方法,因为
// IEnumerable<T> 继承 IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} public class SortedList<T> : MyList<T> where T : IComparable<T>
{
// 一个未优化的简单排序算法,
// 该算法从低到高对列表元素排序:
public void BubbleSort()
{
if (null == head || null == head.Next)
return; bool swapped;
do
{
Node previous = null;
Node current = head;
swapped = false; while (current.next != null)
{
// 由于需要调用此方法,因此,SortedList
// 类在 IEnumerable<T> 上是受约束的
if (current.Data.CompareTo(current.next.Data) > )
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current; if (previous == null)
{
head = tmp;
}
else
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
} else
{
previous = current;
current = current.next;
} }// end while
} while (swapped);
}
} // 一个将自身作为类型参数来实现 IComparable<T> 的简单类,
// 是对象中的
// 常用设计模式,这些对象
// 存储在泛型列表中。
public class Person : IComparable<Person>
{
string name;
int age; public Person(string s, int i)
{
name = s;
age = i;
} // 这会使列表元素
// 按 age 值排序。
public int CompareTo(Person p)
{
return age - p.age;
} public override string ToString()
{
return name + ":" + age;
} // 必须实现 Equals。
public bool Equals(Person p)
{
return (this.age == p.age);
}
} class Generics
{
static void Main(string[] args)
{
// 声明并实例化一个新的范型 SortedList 类。
// Person 是类型参数。
SortedList<Person> list = new SortedList<Person>(); // 创建 name 和 age 值以初始化 Person 对象。
string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" };
int[] ages = new int[] { , , , , , , , , , }; // 填充列表。
for (int x = ; x < names.Length; x++)
{
list.AddHead(new Person(names[x], ages[x]));
} Console.WriteLine("Unsorted List:");
// 打印出未排序的列表。
foreach (Person p in list)
{
Console.WriteLine(p.ToString());
} // 对列表进行排序。
list.BubbleSort(); Console.WriteLine(String.Format("{0}Sorted List:", Environment.NewLine));
// 打印出排序的列表。
foreach (Person p in list)
{
Console.WriteLine(p.ToString());
} Console.WriteLine("Done");
}
} }
1.B.2,
1.C,下载地址返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ylbtech-Unitity-CS:Generics的更多相关文章

  1. ylbtech-LanguageSamples-Generics(泛型)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Generics(泛型) 1.A,示例(Sample) 返回顶部 “泛型”示例 (C#) ...

  2. Class:DbConnectionManipulator.cs

    ylbtech-Class:DbConnectionManipulator.cs 1.返回顶部 1.DbConnectionManipulator.cs using System; using Sys ...

  3. ASP.NET MVC:UrlHelper.cs

    ylbtech-funcation-Utility: ASP.NET MVC:UrlHelper.cs 充当表示 ASP.NET Razor 页的类的基类. 1.UrlHelper 类返回顶部 1-1 ...

  4. ASP.NET MVC:WebPageBase.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebPageBase.cs 充当表示 ASP.NET Razor 页的类的基类. 1.A,WebPageBase 抽象类 ...

  5. ASP.NET MVC:WebPageRenderingBase.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebPageRenderingBase.cs 提供用于呈现使用 Razor 视图引擎的页的方法和属性. 1.A,WebP ...

  6. ASP.NET MVC:WebViewPage.cs

    ylbtech-funcation-Utility: ASP.NET MVC:WebViewPage.cs 表示呈现使用 ASP.NET Razor 语法的视图所需的属性和方法. 1.A,WebVie ...

  7. Unitity 常用工具类

    ylbtech-Unitity_C#: Unitity 常用代码 1.A,效果图返回顶部   1.B,源代码返回顶部 1,日期字符串 using System; using System.Xml; / ...

  8. Class-SP:Order.cs

    ylbtech-Class-SP:Order.cs 1. 返回顶部 1.GoodsType.cs 货品类别 using System; using System.Collections.Generic ...

  9. System.Net.FtpWebRequest.cs

    ylbtech-System.Net.FtpWebRequest.cs 实现文件传输协议(FTP)客户端. 1.返回顶部 1. #region 程序集 System, Version=4.0.0.0, ...

随机推荐

  1. 详解LUA开发工具及其环境配置

    LUA开发工具及其环境配置是本文要介绍的内容,主要是来了解并学习lua开发工具的使用和环境的配置,第一次接触LUA的话,就跟本人一起学习吧.看我能不能忽悠到你. LUA是语言,那么一定有编写的工具.第 ...

  2. 利用 t-SNE 高维数据的可视化

    利用 t-SNE 高维数据的可视化  具体软件和教程见: http://lvdmaaten.github.io/tsne/  简要介绍下用法: % Load data load ’mnist_trai ...

  3. caffe:编译时提示:unsupported GNU version! gcc versions later than 4.9 are not supported!

    NVCC src/caffe/solvers/adam_solver.cuIn file included from /usr/local/cuda/include/cuda_runtime.h:76 ...

  4. /usr/bin/ld: cannot find -lz

    同事在一台机器上新安装的CentOS,我拷贝一个项目在上面编译,老是报如下错误: “/usr/bin/ld: cannot find -lz” 说明:libz.so是有的,在/lib64下面 我设置环 ...

  5. linux node&& npm 安装方式

    1.  编译好的文件 简单说就是解压后,在bin文件夹中已经存在node以及npm,如果你进入到对应文件的中执行命令行一点问题都没有,不过不是全局的,所以将这个设置为全局就好了. cd node-v0 ...

  6. jQuery中怎么添加innerText、innerHtml(转)

    发现如果我在div或者其他非表单的标签中赋值,原本用普通的js就直接document.getElementById("id").innerHtml(或者其他几个)就可以了. 但是在 ...

  7. MySQL分库分表环境下全局ID生成方案 转

    在大型互联网应用中,随着用户数的增加,为了提高应用的性能,我们经常需要对数据库进行分库分表操作.在单表时代,我们可以完全依赖于数据库的自增ID来唯一标识一个用户或数据对象.但是当我们对数据库进行了分库 ...

  8. android数据存储之File

    android中使用File进行存储主要使用到OpenFileOutput和OpenFileInput两个方法,下面直接用一个例子来说明一下. (1)布局文件main.xml文件 <?xml v ...

  9. jQuery Mobile_简单的爱情故事

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. linux更改启动级别后,无法启动的问题解决

    装好之后,配置好IP,启动后也能上网了,然后我修改了系统的启动级别(默认为3,我改为了5),意思是让他能够启动桌面. 我是这么设置的: 1.vi命令打开/etc/inittab文件,可以看到如下描述 ...