c#数组和集合

上传人:第*** 文档编号:53437222 上传时间:2018-08-31 格式:PPT 页数:30 大小:212KB
返回 下载 相关 举报
c#数组和集合_第1页
第1页 / 共30页
c#数组和集合_第2页
第2页 / 共30页
c#数组和集合_第3页
第3页 / 共30页
c#数组和集合_第4页
第4页 / 共30页
c#数组和集合_第5页
第5页 / 共30页
点击查看更多>>
资源描述

《c#数组和集合》由会员分享,可在线阅读,更多相关《c#数组和集合(30页珍藏版)》请在金锄头文库上搜索。

1、第三讲,数组和集合,目标,数组的定义 声明和操作一维和二维数组 交错数组和多维数组 简单的排序方法 数组列表、哈希表、堆栈集合的用法,体验,首先产生一个包含有12个随机数的数组,然后分别计算出数组中的最大值、最小值。最后程序将显示整个随机数数组。随机数的范围是1100之间。,数组 3-1,数组是同一数据类型的一组值 数组属于引用类型,因此存储在堆内存中 数组元素初始化或给数组元素赋值都可以在声明数组时或在程序的后面阶段中进行语法:数据类型元素个数 数组名称;int6 arrayHere;,数组 3-2,学生分数的整数数组,职员姓名的字符串数组,室温的浮点数组,数组位置,数组 3-3,stati

2、c void Main(string args) int count;Console.WriteLine(“请输入您要登记的学生人数 “);count=int.Parse(Console.ReadLine();/ 声明一个存放姓名的字符串数组,其长度等于提供的学生人数 string names = new stringcount;/ 用一个 for 循环来接受姓名 for(int i=0; icount; i+)Console.WriteLine(“请输入学生 0 的姓名 “,i+1);namesi=Console.ReadLine();Console.WriteLine(“已登记的学生如下:

3、 “);/ 用 foreach 循环显示姓名 foreach(string disp in names)Console.WriteLine(“0“, disp); ,数组声明,初始化数组元素的循环,显示输出的循环,一维数组,声明并初始化 int array1 = new int5 1, 3, 5, 7, 9 ; string weekDays = new string “Sun“, “Mon“, “Tue“, “Wed“, “Thu“, “Fri“, “Sat“ ; int array2 = 1, 3, 5, 7, 9 ; string weekDays2 = “Sun“, “Mon“, “T

4、ue“, “Wed“, “Thu“, “Fri“, “Sat“ ; 声明一个数组变量但不将其初始化 int array3; array3 = new int 1, 3, 5, 7, 9 ; / OK /array3 = 1, 3, 5, 7, 9; / Error,多维数组,声明数组 int, array6 = new int10, 10; 声明数组时将其初始化 int, array2D = new int, 1, 2 , 3, 4 , 5, 6 , 7, 8 ; int, , array3D = new int, 1, 2, 3 , 4, 5, 6 ; 声明一个数组变量但先不将其初始化 in

5、t, array5; array5 = new int, 1, 2 , 3, 4 , 5, 6 , 7, 8 ; / OK /array5 = 1,2, 3,4, 5,6, 7,8; / Error 也可以给数组元素赋值,例如: array52, 1 = 25;,交错数组3-1,交错数组是数组的元素为数组的数组,6,2,3,5,交错数组声明 int jaggedArray = new int4; 交错数组初始化 jaggedArray0 = new int6; jaggedArray1 = new int2; jaggedArray2 = new int3; jaggedArray3 = ne

6、w int5;,交错数组3-2,例如: jaggedArray0 = new int 1, 3, 5, 7, 9,13 ; jaggedArray1 = new int 0, 2, ; jaggedArray2 = new int 5,11, 22 ; jaggedArray3 = new int 3,5,7,10, 32 ;还可以在声明数组时将其初始化,如: int jaggedArray2 = new int new int 1,3,5,7,9,new int 0,2,4,6,new int 11,22 ;,交错数组3-3,访问交错数组 jaggedArray301 = 77;jagged

7、Array321 = 88;,数组示例,using System; class DeclareArraysSample public static void Main()/ 一维数组int numbers = new int5;/ 多维数组string, names = new string5, 4;/ 数组的数组(交错数组)byte scores = new byte5;/ 创建交错数组for (int i = 0; i scores.Length; i+)scoresi = new bytei + 3;/ 打印每行的长度for (int i = 0; i scores.Length; i+

8、)Console.WriteLine(“Length of row 0 is 1“, i, scoresi.Length); ,数组排序-气泡,将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮。,【示例】: 49 13 13 13 13 13 13 13 38 49 27 27 27 27 27 27 65 38 49 38 38 38 38 38 97 65 38 49 49 49 49 49 76 97 65 49 49 49 49 49 13 76 97 65 65 65 65 65 27 27 76 97 76 76 76 76,/气泡排序 using Syste

9、m; namespace BubbleSorter public class BubbleSorterpublic void Sort(int list)int i, j, temp;bool done = false;j = 1;while (j listi + 1)done = false;temp = listi;listi = listi + 1;listi + 1 = temp;j+;,public class MainClasspublic static void Main()int iArrary = new int 1, 5, 13, 6, 10, 55, 99, 2, 87,

10、 12, 34, 75, 33, 47 ;BubbleSorter sh = new BubbleSorter();for (int m = 0; m iArrary.Length; m+)Console.Write(“0 “, iArrarym);Console.WriteLine();sh.Sort(iArrary);for (int m = 0; m iArrary.Length; m+)Console.Write(“0 “, iArrarym);Console.WriteLine(); ,集合,System.Collections命名空间,数组列表集合-ArrayList,类似一维数组

11、 数组列表是动态数组 可以存放任何对象 常用方法: 增加元素-Add 插入元素-Insert 删除元素-Remove,ArrayList示例,ArrayList myAL = new ArrayList(); myAL.Add(“Hello“); myAL.Add(“World“); myAL.Add(“!“); Console.WriteLine(“myAL“); Console.WriteLine(“ Count: 0“, myAL.Count); Console.WriteLine(“ Capacity: 0“, myAL.Capacity); Console.Write(“ Value

12、s:“); PrintValues(myAL);,public static void PrintValues(IEnumerable myList)foreach (Object obj in myList)Console.Write(“ 0“, obj);Console.WriteLine();,位数组集合-BitArray,BitArray集合是位值(位值是1和0)的组合体 BitArray位集合是其元素为位标志的集合 每一元素都是一位,而不是一个对象 BitArray容量始终与计数相同 提供位运算方法,例如And、Or、Xor、Not和SetAll 等,1 0 0 1 1 1 0,Le

13、ngth,BitArray flags = new BitArray(7) flags.Set(0,true); flags.Set(1,false); bool IsMarry = flags.Get(3);,BitArray-示例,using System; using System.Collections; public class SamplesBitArray public static void Main()BitArray myBA1 = new BitArray(5);BitArray myBA2 = new BitArray(5);myBA10 = myBA11 = fals

14、e;myBA12 = myBA13 = true;myBA20 = myBA22 = false;myBA21 = myBA23 = true;myBA21 = myBA24 = true;Console.WriteLine(“初始值:“);Console.Write(“myBA1:“);PrintValues(myBA1, 8);Console.Write(“myBA2:“);PrintValues(myBA2, 8);Console.WriteLine();Console.WriteLine(“结果:“);Console.Write(“位与操作AND:“);PrintValues(myBA

15、1.And(myBA2), 8);Console.WriteLine();Console.WriteLine(“After AND“);Console.Write(“myBA1:“);PrintValues(myBA1, 8);Console.Write(“myBA2:“);PrintValues(myBA2, 8);Console.WriteLine();,tryBitArray myBA3 = new BitArray(8);myBA30 = myBA31 = myBA32 = myBA33 = false;myBA34 = myBA35 = myBA36 = myBA37 = true;

16、myBA1.And(myBA3);catch (Exception myException)Console.WriteLine(“Exception: “ + myException.Message);public static void PrintValues(IEnumerable myList, int myWidth)int i = myWidth;foreach (Object obj in myList)if (i = 0)i = myWidth;Console.WriteLine();i-;Console.Write(“0,8“, obj);Console.WriteLine(); ,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号