lwj_C#_泛型使用
来源:互联网 发布:聊骚软件靠谱吗 编辑:程序博客网 时间:2024/04/30 19:33
public static class Test2
{
//数组 转成(拼接) 字符串;
public static string CompoundString<T>(T[] arr)
{
string str = "";//null;
foreach (T t in arr)
{
str += (t.ToString() + " ");
}
return str;
}
}
public class Test {
//冒泡排序 //实现接口
public static void Sort <T> (T[] array) where T : IComparable{
for (int i = 0; i < array.Length-1; i++)
{
for (int j = 0; j < array.Length - i - 1; j++)
{
if (array[j].CompareTo(array[j+1]) == 1)
{
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
//foreach (T i in array)
//{
// Console.WriteLine(i);
//}
}
}
//结构体
public struct STest : IComparable
{
public float s;
public int CompareTo(object obj)
{
STest st = (STest)obj;
//自己写的比较
if (this.s > st.s)
{
return 1;
}
else if (this.s < st.s)
{
return -1;
}
else
{
return 0;
}
}
}
class MainClass
{
public static void Main(string[] args)
{
string str = Test2.CompoundString<string>(new[] { "jiangkai", "shi", "sb" });
Console.WriteLine(str);
Test.Sort<string>(new[]{"dfsd","jkm","Dhf","erter"});
Test.Sort<int>(new[]{767,342,3,12,4545,1});
STest st_1 = new STest();
st_1.s = 12f;
STest st_2 = new STest();
st_2.s = 24f;
STest st_3 = new STest();
st_3.s = 65f;
int a = st_1.CompareTo(st_2);
Console.WriteLine(a);
//结构体数组
STest [] st = { st_1, st_2, st_3 };
Test.Sort<STest>(st);
foreach (STest i in st)
{
Console.WriteLine(i.s);
}
}
}
{
//数组 转成(拼接) 字符串;
public static string CompoundString<T>(T[] arr)
{
string str = "";//null;
foreach (T t in arr)
{
str += (t.ToString() + " ");
}
return str;
}
}
public class Test {
//冒泡排序 //实现接口
public static void Sort <T> (T[] array) where T : IComparable{
for (int i = 0; i < array.Length-1; i++)
{
for (int j = 0; j < array.Length - i - 1; j++)
{
if (array[j].CompareTo(array[j+1]) == 1)
{
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
//foreach (T i in array)
//{
// Console.WriteLine(i);
//}
}
}
//结构体
public struct STest : IComparable
{
public float s;
public int CompareTo(object obj)
{
STest st = (STest)obj;
//自己写的比较
if (this.s > st.s)
{
return 1;
}
else if (this.s < st.s)
{
return -1;
}
else
{
return 0;
}
}
}
class MainClass
{
public static void Main(string[] args)
{
string str = Test2.CompoundString<string>(new[] { "jiangkai", "shi", "sb" });
Console.WriteLine(str);
Test.Sort<string>(new[]{"dfsd","jkm","Dhf","erter"});
Test.Sort<int>(new[]{767,342,3,12,4545,1});
STest st_1 = new STest();
st_1.s = 12f;
STest st_2 = new STest();
st_2.s = 24f;
STest st_3 = new STest();
st_3.s = 65f;
int a = st_1.CompareTo(st_2);
Console.WriteLine(a);
//结构体数组
STest [] st = { st_1, st_2, st_3 };
Test.Sort<STest>(st);
foreach (STest i in st)
{
Console.WriteLine(i.s);
}
}
}
阅读全文
0 0
- lwj_C#_泛型使用
- lwj_C#_静态,单例的使用
- lwj_C#_排序
- lwj_C#_作业
- lwj_C#_接口
- lwj_C#_字典
- lwj_C#_类的相互使用和构造方法
- lwj_C#_集合的使用、接口方法的实现;
- lwj_C#_周总结1
- lwj_C#_集合list<T>
- lwj_C#_方法重载,递归,构造
- lwj_C#_周总结2 字符串练习
- lwj_C#_作业 List<T>应用
- lwj_C#_建立一个数学MathTool类包含的方法
- lwj_C#_类的属性,方法参数 举例习题
- lwj_C#_集合stack栈和queue队列
- lwj_C#_work 字符串、类的使用和数学运算
- lwj_C#_输入年月日,写方法输出它是今年的第几天;
- Andrew Coursera Machine Learning
- [Android面试]==和 equals 的区别
- 判断app是否启动
- PHP 解决子域名请求跨域问题
- 多模态与图像文本匹配
- lwj_C#_泛型使用
- 【统计学】t-检验
- 从键盘输入一个字符,判断其是字母字符,还是数字字符,还是其他字符
- 1.11 异常处理:处理错误
- android studio快捷键
- 【SSH网上商城】拦截器实现后台权限校验
- 存储过程--首次尝试
- HTML5是什么?为你详细解读神奇的HTML5!
- Android_Handler机制