C#学习笔记- 浅谈数组复制,排序,取段,元组
作者:jingxian 时间:2021-10-21 00:50:58
C#学习笔记- 浅谈数组复制,排序,取段,元组
using System;
using System.Collections.Generic;
namespace Application
{
class Test
{
static void Main ()
{
//元组类型Tuple是静态类型,用静态方法创建实例,超过8个元素则第8个元素是元组类型
var tupe = Tuple.Create<int,int,string,string> (1, 2, "a", "b");
Console.WriteLine ("{ 0},{ 1}",tupe.Item1, tupe.Item3);
//=====Array类是抽象类,只能通过它的静态方法CreateInstance()创建实例
//=====如果事先不知道类型,可以用此方法
Array arrays = Array.CreateInstance (typeof(int), 5);
for (int i = 0; i < arrays.Length; i++) {
arrays.SetValue (35, i);
Console.WriteLine (arrays.GetValue (i));
}
Person[] persons = {
new Person { firstName = "su", lastName = "uifu" },
new Person { firstName = "chen", lastName = "xaohua" },
new Person { firstName = "cbb", lastName = "ruifu" },
new Person { firstName = "utt", lastName = "xiaohua" }
} ;
//=====Clone()复制数组,引用类型复制索引值,值类型复制值
Person[] persons1 = persons.Clone ();
Array.Sort (persons, new PersonComparer (PersonCompareType.lastName));
foreach (var p in persons) {
Console.WriteLine (p);
}
//======ArraySegment<T>对数组取段====
var segments = new ArraySegment<Person> (persons, 1, 2);
for (int i = segments.Offset; i < segments.Offset + segments.Count; i++) {
Console.WriteLine (segments.Array [i]);
}
}
public class Person
{
public string firstName{ get; set; }
public string lastName{ get; set; }
public override string ToString ()
{
return String.Format ("{ 0},{ 1}", firstName, lastName);
}
}
//======要对引用类型的数组使用Array.sort()方法,必须对类实现IComparable<T>接口
//======或写一个附加类并实现Comparer<T>接口
public enum PersonCompareType
{
firstName,
lastName
}
public class PersonComparer:IComparer<Person>
{
private PersonCompareType pct;
public PersonComparer (PersonCompareType pct)
{
this.pct = pct;
}
public int Compare(Person x,Person y)
{
if (x == null)
throw new ArgumentNullException ();
if (y == null)
throw new ArgumentNullException ();
switch (pct) {
case PersonCompareType.firstName:
return x.firstName.CompareTo (y.lastName);
case PersonCompareType.lastName:
return x.lastName.CompareTo (y.lastName);
default:
throw new ArgumentException ("no..");
}
}
}
}
标签:C#,元组,数组
0
投稿
猜你喜欢
Spring整合SpringMVC与Mybatis(SSM)实现完整登录功能流程详解
2022-07-15 10:19:25
git工作区和暂存区_动力节点Java学院整理
2022-06-27 20:31:06
C#串口通信实现方法
2023-04-15 06:31:11
详解SpringMVC中的异常处理
2022-10-22 07:05:14
Java实现调用对方http接口得到返回数据
2023-02-27 22:36:29
C#使用Unity实现剪刀石头布游戏
2023-03-01 06:30:18
通过实例解析Spring argNames属性
2023-09-14 10:43:13
纯Java代码实现流星划过天空
2022-06-01 12:12:35
Android实现遮罩层(蒙板)效果
2023-04-26 18:43:03
java与JSON数据的转换实例详解
2022-07-03 22:48:25
C#中using语句的用法
2023-07-02 05:33:24
2021年最新Redis面试题汇总(3)
2022-02-27 13:31:22
win10下配置java环境变量的方法
2022-08-23 17:10:11
C#集合之并发集合的用法
2023-12-24 09:30:21
Java Controller实现参数验证与统一异常处理流程详细讲解
2022-01-25 18:49:47
解析c#操作excel后关闭excel.exe的方法
2021-08-14 16:55:22
封装的android监听手指左右滑动屏幕的事件类分享
2021-10-21 07:10:08
解决mybatis-plus自动配置的mapper.xml与java接口映射问题
2023-08-25 04:16:02
Java实现宠物商店管理
2023-09-14 09:14:13
C#实现读取匿名对象属性值的方法示例总结
2023-02-02 18:56:03