C#数组和元组
声明数组
int[] myArray;
初始化数组
myArray = new int[4];
数组是引用类型当初始化完毕后,将在托管堆上分配内存空间,其结构图如下
声明和初始化放在一起
int[] myArray = new int[4]
int[] myArray = new int[4] {4, 7, 11, 2};
int[] myArray = new int[] {4, 7, 11, 2};
int[] myArray = {4, 7, 11, 2};
引用类型数组
下面自定义一个person类
public class person
{
public string firstname{get;set;}
public string lastname{get;set;}
public override string ToString()//重写了基类的string类
{
return string.fromat("{0},{1}",firstname,lastname);
}
}
现在定义一个person数组
Person[] myPersons = new Person[2];
对每一个元素进行初始化
myPersons[0] = new Person { FirstName="Ayrton", LastName="Senna" };
myPersons[1] = new Person { FirstName="Michael", LastName="Schumacher" };
也可以这样定义
Person[] myPersons2 =
{
new Person { FirstName="Ayrton", LastName="Senna"},
new Person { FirstName="Michael", LastName="Schumacher"}
};
该数组在内存中的存储结构
多维数组
二维数组
int[,] twodim = new int[3, 3];
twodim[0, 0] = 1
定义兼赋值
int[,] twodim = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
三维数组
int[,,] threedim = {
{ { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } },
{ { 9, 10 }, { 11, 12 } }
};
锯齿数组
int[][] jagged = new int[3][];
jagged[0] = new int[2] { 1, 2 };
jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
jagged[2] = new int[3] { 9, 10, 11 };
数组类 array class
array是抽象类型不能够new
Array intArray1 = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
{
intArray1.SetValue(33, i);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(intArray1.GetValue(i));
}
也可以把该数组转换为传统c#定义的int数组
int[] intArray2 = (int[])intArray1;
createInstance有好多重载函数
创二维数组
int[] lengths = { 2, 3 };
int[] lowerBounds = { 1, 10 };
Array array2 = Array.CreateInstance(typeof(Person), lengths, lowerBounds);
array2.SetValue(new Person
{
FirstName = "tian",
LastName = "hello"
},1,10);
也可以对此数组进行转换
Person[,] racers2 = (Person[,])racers;
Person first = racers2[1, 10];
Person last = racers2[2, 12];
数组的复制
因为数组是引用类型,把数组的名称直接赋给另一个数组其实就是把引用赋给了这个数组他们还是指向同一个数组
如果数组的元素类型是值类型可以通过下面方法进行复制
int[] intArray1 = {1, 2};
int[] intArray2 = (int[])intArray1.Clone();
这两个数组在内存中的存储结构如下
如果数组包括引用类型也只是引用的复制,他们指向的还是同一个对象
Person[] beatles = {
new Person { FirstName="John", LastName="Lennon" },
new Person { FirstName="Paul", LastName="McCartney" }
};
Person[] beatlesClone = (Person[])beatles.Clone();
其中person是自定义的类类型
其在内存中的存储结构如下
clone方法是数组的浅复制,如果想要对包括引用类型的数组进行deep copy,那么就必须遍历数组然后对数组每一项进行创建新对象
数组的比较
public class Person : IComparable<Person>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
}
public int CompareTo(Person other)
{
if (other == null) return 1;
int result = string.Compare(this.FirstName, other.FirstName);
if (result == 0)
{
result = string.Compare(this.LastName, other.LastName);
}
return result;
}
}
被比较数组的元素必须要实现icomparable接口
static void Main(string[] args)
{
Person[] persons =
{
new Person{FirstName = "Damon",LastName = "Hill"},
new Person{FirstName = "Niki",LastName = "Lauda"},
new Person{FirstName = "Ayrton" ,LastName ="Senna"},
new Person{FirstName = "Graham",LastName = "Hill"}
};
Array.Sort(persons);
foreach(var pr in persons)
{
Console.WriteLine(pr);
}
}
元组(tuples)
数组包含是相同类型的元素而元组可以包含不同类型的元素
static void Main(string[] args)
{
var result = Divide(5, 2);
Console.WriteLine("result of division:{0},remender:{1}", result.Item1, result.Item2);
var tuple = Tuple.Create<string, string, string, int, int, int,double, Tuple<int, int>>("tian", "mo", "chou", 4, 5, 6, 2.3,Tuple.Create<int, int>(20, 20));
;
}
public static Tuple<int,int> Divide(int dividend,int divisor)
{
int result = dividend / divisor;
int reminder = dividend % divisor;
return Tuple.Create<int, int>(result, reminder);
}
C#数组和元组的更多相关文章
- 小甲鱼:Python学习笔记002_数组_元组_字符串
创建普通数组 >>> member=["山东黄金","九阳股份"] >>> member ['山东黄金', '九阳股份'] ...
- C#学习笔记二 (资源托管,泛型,数组和元组,运算符和类型强制转换)
托管和非托管资源 1.托管资源是指GC管理的内存空间,非托管资源是指文件句柄,网络连接,数据库连接等. 2.方法中临时申请的变量,被存放在栈中.栈存储非对象成员的值数据.例如在方法中有B b=new ...
- Scala学习——数组/映射/元组
[<快学Scala>笔记] 数组 / 映射 / 元组 一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初 ...
- Scala详解---------数组、元组、映射
一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初始值的数组,无需new关键字 Scala声明数组时,需要带有Arr ...
- Scala具体解释---------数组、元组、映射
一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初始值的数组,无需newkeyword Scala声明数组时.须要带 ...
- TypeScript学习——数组、元组、接口(2)
数组 数组类型注解 const numberArr: (number | string)[] = [1, '2', 3]; //既可以是number 也可以是string const stringAr ...
- Python 数组[],元组(),字典{}的异同
序列 Python有6中内建的序列,在这里我们重点讨论两种,数组和元组.所有序列都可以做某些特定的操作,大致上常用的是:加,乘,索引,分片以及检查某个元素是否属于序列的成员. Python还提供一些内 ...
- C#高级编程笔记 Day 8, 2016年9月 28日 (数组)
1.数组的初始化 声明了数组后,就必须为数组分配内存,以保存数组的所有元素.数组是引用类型,所以必须给它分配堆上的内存,为此,应使用 new 运算符,指定数组中元素的类型和数量来初始化数组的变量.若使 ...
- 浅谈C#数组(二)
六.枚举集合 在foreach语句中使用枚举,可以迭代集合中的元素,且无需知道集合中元素的个数.foreach语句使用一个枚举器.foreach会调用实现了IEnumerable接口的集合类中的Get ...
随机推荐
- 消息推送之androidpn部署
androidpn客户端的部署在eclipse上,要记住两点: 1.直接导入的项目是上个世纪的lib这个文件夹,要改为libs.否则会报错找不到云云类. 2.如果在虚拟器上测试,要将res/raw/a ...
- CFRound#379(div2)
题目链接:http://codeforces.com/contest/734 A:SB题. #include<cstdio> #include<cstring> #includ ...
- tuple 可更改的列表和不可更改的元组
tuple([iterable]) Return a tuple whose items are the same and in the same order as iterable's items. ...
- eclipse 一些快捷键
快捷键 alt + 上下方向键 向后缩进 shift + tab 整体向左移动 tab 就是向右移动 ctrl + Q 就是构建有参的构造方法 ctrl + E 是get set 方法,要把quick ...
- 使用FormData,进行Ajax请求并上传文件
前段时间做了个手机端的图片上传,为了用户体验,用ajax交互,发现了FromData对象,这里有详细解释https://developer.mozilla.org/zh-CN/docs/Web/API ...
- PHP在浏览器上跟踪调试的方法以及使用ChromePhp、FirePHP的简单介绍
之前用ThinkPHP时发现有个 trace 函数可以跟踪调试,感觉很有意思,网上搜索了下类似的东西,发现了 ChromePhp ,以前没想过这样来调试 PHP 程序,感觉非常方便,很有用. Thin ...
- windows 开机自动启动方案
方案1: 把要启动的软件的快捷方式放到启动菜单对应的目录里,就像下面这个路径: C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Sta ...
- [Q]无法卸载怎么办
正确卸载CAD批量打图精灵的方法是进入操作系统,“控制面版”,然后运行“添加和删除程序”,找到CAD批量打图精灵,选“更改/删除”,按照提示操作,即可进行卸载. 若使用强制卸载工具(如360等)卸载可 ...
- ActiveMQ in Action(6) - Features
关键字: activemq 2.6 Features ActiveMQ包含了很多功能强大的特性,下面简要介绍其中的几个.2.6.1 Exclusive Consumer Queue中的消息 ...
- 修改LibreOffice Draw中定义的样式名称
目前我使用的是LibreOffice 4.2.4.2.经过以往的测试和使用经验,这是诸多版本中较为稳定和bug相对较少的.今天无意中发现该版本的LibreOffice Draw存在一个问题:样式名称修 ...