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#,元组,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaSE中compare、compareTo的区别
2021-12-28 09:37:47
![](https://img.aspxhome.com/file/2023/4/77134_0s.png)
Java利用移位运算将int型分解成四个byte型的方法
2023-11-09 08:25:00
![](https://img.aspxhome.com/file/2023/4/59334_0s.png)
Spring中XML schema扩展机制的深入讲解
2022-06-29 07:44:15
![](https://img.aspxhome.com/file/2023/4/77384_0s.png)
java 集合----Map、Collection
2022-11-09 03:39:01
java实现简单的英文文本单词翻译器功能示例
2023-11-28 10:22:15
![](https://img.aspxhome.com/file/2023/8/59178_0s.png)
MyBatis常用标签以及使用技巧总结
2022-02-27 20:52:14
Spring Boot 配置和使用多线程池的实现
2022-09-04 19:53:02
JAVA得到数组中最大值和最小值的简单实例
2023-03-18 01:19:39
Springboot之idea之pom文件图标不对问题
2021-12-31 07:27:32
![](https://img.aspxhome.com/file/2023/0/62520_0s.png)
Java中两种基本的输入方式小结
2023-02-06 18:37:24
![](https://img.aspxhome.com/file/2023/2/71642_0s.jpg)
Java 实现分布式服务的调用链跟踪
2023-11-25 10:24:55
![](https://img.aspxhome.com/file/2023/4/60124_0s.png)
java当中的定时器的4种使用方式
2022-08-07 14:31:39
C#实现的海盗分金算法实例
2023-12-20 21:00:53
C#实现闪动托盘图标效果的方法
2023-10-04 01:58:47
Java中常见死锁与活锁的实例详解
2023-06-26 13:17:00
SWT(JFace)体验之ViewForm的使用
2023-10-20 13:02:01
![](https://img.aspxhome.com/file/2023/6/89566_0s.jpg)
Java中checkbox实现跨页多选的方法
2023-10-14 11:01:20
java基础(System.err和System.out)详解
2022-10-23 23:27:37
springboot中bean的加载顺序问题
2022-01-04 19:55:57
![](https://img.aspxhome.com/file/2023/0/62510_0s.png)
基于javaWeb 项目SSM配置要点及可能遇到的问题和解决方法
2023-10-27 21:45:16