5.数组-C#高级编程
2016-12-10 18:15
92 查看
本文固定连接:http://blog.csdn.net/u013108312/article/details/53560101
数组作为参数:
using System; namespace Wrox.ProCSharp.Arrays { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format("{0} {1}", FirstName, LastName); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Wrox.ProCSharp.Arrays { class Program { static void Main() { SimpleArrays(); TwoDim(); ThreeDim(); Jagged(); ArrayClass(); CopyArrays(); } static void CopyArrays() { Person[] beatles = { new Person { FirstName="John", LastName="Lennon" }, new Person { FirstName="Paul", LastName="McCartney" } }; Person[] beatlesClone = (Person[])beatles.Clone(); } static void ArrayClass() { 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)); } int[] lengths = { 2, 3 }; int[] lowerBounds = { 1, 10 }; Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds); racers.SetValue(new Person { FirstName = "Alain", LastName = "Prost" }, index1: 1, index2: 10); racers.SetValue(new Person { FirstName = "Emerson", LastName = "Fittipaldi" }, 1, 11); racers.SetValue(new Person { FirstName = "Ayrton", LastName = "Senna" }, 1, 12); racers.SetValue(new Person { FirstName = "Michael", LastName = "Schumacher" }, 2, 10); racers.SetValue(new Person { FirstName = "Fernando", LastName = "Alonso" }, 2, 11); racers.SetValue(new Person { FirstName = "Jenson", LastName = "Button" }, 2, 12); Person[,] racers2 = (Person[,])racers; Person first = racers2[1, 10]; Person last = racers2[2, 12]; } static void Jagged() { 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 }; for (int row = 0; row < jagged.Length; row++) { for (int element = 0; element < jagged[row].Length; element++) { Console.WriteLine( "row: {0}, element: {1}, value: {2}", row, element, jagged[row][element]); } } } static void ThreeDim() { int[, ,] threedim = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }, { { 9, 10 }, { 11, 12 } } }; Console.WriteLine(threedim[0, 1, 1]); } static void TwoDim() { int[,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; } static void SimpleArrays() { 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"} }; } } }
数组作为参数:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ArraySegmentSample { class Program { static void Main() { int[] ar1 = { 1, 4, 5, 11, 13, 18 }; int[] ar2 = { 3, 4, 5, 18, 21, 27, 33 }; var segments = new ArraySegment<int>[2] { new ArraySegment<int>(ar1, 0, 3), new ArraySegment<int>(ar2, 3, 3) }; var sum = SumOfSegments(segments); Console.WriteLine("sum of all segments: {0}", sum); } static int SumOfSegments(ArraySegment<int>[] segments) { int sum = 0; foreach (var segment in segments) { for (int i = segment.Offset; i < segment.Offset + segment.Count; i++) { sum += segment.Array[i]; } } return sum; } } }
相关文章推荐
- C#高级编程三十二天----数组基础
- pp看书笔记---C#高级编程第九版 第六章 【数组】
- 6.数组 枚举 -C#高级编程
- 《C#高级编程》【第六章】数组 -- 学习笔记
- 《C#高级编程》读书笔记(四):关于数组
- C#高级编程五十七天----位数组
- C#高级编程五十七天----位数组
- C#高级编程七十六天----使用指针实现基于栈的高性能数组
- C中数组的另类引用
- 深入解析 数组 和 指针
- c语言-数组/指针/函数(小结)
- UNICODE十六进制数组转成中英文
- hdu 2688 Rotate(树状数组)
- shell 数组用法
- 对一个结构体或类的数组进行排序
- 一维和二维子数组之和最大值
- 指针数组(指向指针的指针)空间的释放
- 栈—数组与链表实现
- 异或的应用 及剑指offer 面试 40 数组中只出现一次的数字
- 剑指offer--把数组排成最小的数