数据类型数组和字符串ppt课件.ppt

上传人:sccc 文档编号:5353578 上传时间:2023-06-28 格式:PPT 页数:60 大小:451.51KB
返回 下载 相关 举报
数据类型数组和字符串ppt课件.ppt_第1页
第1页 / 共60页
数据类型数组和字符串ppt课件.ppt_第2页
第2页 / 共60页
数据类型数组和字符串ppt课件.ppt_第3页
第3页 / 共60页
数据类型数组和字符串ppt课件.ppt_第4页
第4页 / 共60页
数据类型数组和字符串ppt课件.ppt_第5页
第5页 / 共60页
点击查看更多>>
资源描述

《数据类型数组和字符串ppt课件.ppt》由会员分享,可在线阅读,更多相关《数据类型数组和字符串ppt课件.ppt(60页珍藏版)》请在三一办公上搜索。

1、-数据类型、数组和字符串,C#程序设计,数据类型,C#中数据类型代表了要在变量中存储的数据的种类,每一个变量或对象都必须申明为一种数据类型。C#数据类型可分为基本类型(比如int,char,double等)用户自定义的类型(struct,class,interface等),C#基本类型,byte、char、short、int、longfloat、double、decimalboolstring,Integral types,Integral types,char char1=A;/Character literal char char2=x0041;/Hexadecimal char char

2、3=(char)65;/Cast from integral type char char4=u0041;/Unicode,Floating-point types,float x=3.5;/errorfloat x=3.5F;/okfloat x=(float)3.5;/okdouble y=3;/ok,decimal,decimal myMoney=300.5m;/okdecimal myMoney=300.5;/errordecimal myMoney=(decimal)300.5;/okdecimal myMoney=300;/ok,bool,bool类型只有两个取值(true和fal

3、se),using System;public class MyClass static void Main()bool flag=true;Console.WriteLine(flag);char c=0;bool Alphabetic=(c 64,bool,int x=123;if(x)/Invalid in C#printf(The value of x is nonzero.);,int x=123;if(x!=0)/The C#way Console.Write(The value of x is nonzero.);,string,定义并初始化 string s1=“Welcome

4、”;string s2=“To”;string s3=s1+s2+”C#”;,用户自定义类型,struct,class,struct Book public decimal price;public string title;public string author;Book b;b.price=30.5Mb.title=“Gone with the wind”;b.author=“Margaret”;,class Book public decimal price;public string title;public string author;Book b;b=new Book();b.p

5、rice=30.5Mb.title=“Gone with the wind”;b.author=“Margaret”;,值类型和引用类型,从保存的数据形式来看,C#数据类型又可分为Value Types(值类型)Reference Types(引用类型),Value Types(值类型),变量直接存储“值”,并置于栈内存中。,int num1;int num2;num1=7;num2=num1;,栈内存,堆内存,num1,num2,7,Value Types(值类型),C#中的值类型byte、char、short、int、longfloat、double、decimalboolstruct(结

6、构体)enum(枚举),struct(结构体),struct Book public decimal price;public string title;public string author;Book b,c;b.price=30.5Mb.title=“Gone with the wind”;b.author=“Margaret”;c=b;,堆内存,b.author,b.title,栈内存,b.price,Margaret,Gone with the wind,30.5,c.author,c.title,c.price,Value Types(值类型),Reference Types(引用

7、类型),引用类型的变量存储的是对象地址,而不是对象本身。,string str1=“Hello”;sting str2;,栈内存,堆内存,str1,0 x123FE,0 x123FE,null,str2,Reference Types(引用类型),引用类型的变量存储的是对象地址,而不是对象本身。,string str1=“Hello”;string str2=null;,栈内存,堆内存,Str1,0 x123FE,0 x123FE,null,Str2,str2=str1;?,Reference Types(引用类型),引用类型的变量存储的是对象地址,而不是对象本身。,string str1=“

8、Hello”;string str2=null;,栈内存,堆内存,Str1,0 x123FE,0 x123FE,0 x123FE,Str2,str2=str1;,Reference Types(引用类型),C#中的引用类型stringclassinterfacedelegate,BankCustomer c;c=new BankCustomer();.c=new BankCustomer();.,Reference Types(引用类型),class,栈内存,堆内存,基本数据类型的别名,C#基本数据类型在名字空间System中的别名,将用户输入的数据转换成int类型,int num;num=C

9、onvert.ToInt32(Console.ReadLine();或:num=int.Parse(Console.ReadLine();,Console.ReadLine()用于从用户那里接受输入作为一个字符串。,类型间转换,字符串类型(string),C#中内置的数据类型string不是一个值类型,而是一个引用类型,它被用来表示字符序列。string将字符串当作一个整体来处理,不能修改串中的字符元素,可看作一个字符串常量。如“This is a string.n”。,定义并初始化 string s;s=“hello,world”;或:string s=“hello,world”;,字符串类

10、型(string),str2=str1;/赋值后存储器的映象又如何?,字符串类型(string),栈内存,堆内存,str1,str2,1,2,3,a,b,c,0088:4400,0088:4400,0088:4660,0088:4660,str1标识的对象,y,x,7,7,str2标识的对象,字符串的存储形式:int x=7;int y=7;string str1=“abc”;string str2=“123;,栈内存,堆内存,str1,str2,1,2,3,a,b,c,0088:4400,0088:4400,0088:4400,0088:4660,st2、str1标识的对象,y,x,7,7,

11、str1赋给str2后存储器的映象图,字符串类型(string),字符串的连接运算符+:,栈内存,堆内存,3,123,abc,abc123,字符串类型(string),string str1=“abc”;string str2=“123”;string str3=str1+str2;,str1+=str2;?,字符串的连接运算符+:,栈内存,堆内存,3,123,abc,abc123,str1,str2,0088:4400,0088:4400,str3,0088:6600,字符串类型(string),string str1=“abc”;string str2=“123”;string str3=

12、str1+str2;str1+=str2;,abc123,=运算符 它是用来比较两个字符串的串值或串址是否相等。,字符串类型(string),string str1=“abc”;string str2=“a”;str2+=“bc”;if(str1=str2)if(object)str1=(object)str2);,取子串,字符串类型(string),string s1=orange;string s2=red;s1+=s2;System.Console.WriteLine(s1);/outputs orangered s1=s1.Substring(2,5);System.Console.W

13、riteLine(s1);/outputs anger,通过 运算符获取串中单个字符,字符串类型(string),string str=Printing backwards;char x=str4;/x=t;for(int i=0;i str.Length;i+)System.Console.Write(strstr.Length-i-1);/outputs sdrawkcab gnitnirP,用IndexOf()方法,查找子串,字符串类型(string),string s9=Battle of Hastings,1066;/outputs 10System.Console.WriteLin

14、e(s9.IndexOf(Hastings);/outputs-1System.Console.WriteLine(s9.IndexOf(1967);,用Split()方法分解串中单词,字符串类型(string),char delimit=new char;string s10=The cat sat on the mat.;string words=s10.Split(delimit);foreach(string substr in words)System.Console.WriteLine(substr);,输出结果Thecatsatonthemat.,字符串类型(string),在.

15、NET Framework中string是类String的别名方法:下表介绍一些最常用的String 类的方法,1、将任意一字符串反序输出,并且每个字符都大写。2、求将某一字符串str中的所有子序列smod都删除后所得的新串newStr?例如:str=“H234llo,Wo234rld234!”;smod=“234”;求新串 newStr?,字符串类型(string),思考题?,数组(Array),数组类型属于复合数据类型,它是由一组顺序存储的同一类型的元素组成的数据集合。特点:元素类型相同;元素有顺序;所有元素共用一个名称。,数组的声明,格式:type arrayName;举例:int nu

16、m;double array_double;string str;Point P;,数组的创建,在C#中,声明数组时不能指定它的长度,而是利用new 运算符来为数组型变量分配内存空间,我们将其称之为创建数组。num=new int3;array_double=new double1000;string str=new string5;数组创建后,系统自动为数组元素赋初值。,数组元素默认值,数组创建后的元素默认初值。,数组(Array),基本数据类型一维数组内存分配,栈内存,堆内存,num,0,0,0,0088:4400,0088:4400,new int3产生的对象,int num;num=n

17、ew int3;,数组(Array),基本数据类型一维数组内存分配,栈内存,堆内存,num,0,0,0,null,0088:4400,new int3产生的对象,int num;num=new int3;num=null;,数组(Array),基本数据类型一维数组初始化,class TestArrays public static void Main()int array1;array1=new int3;/默认值为0 int array2=new int3;int array3=5,9,10;int array4=new int35,9,10;int array5=array4;/两数组指向

18、同一内存区,数组(Array),对象数组的内存分配,class Student private string name;private int age;public Student(string name,int age)this.name=name;this.age=age;,栈内存,堆内存,students,null,Student students;,数组(Array),对象数组的内存分配,堆内存,students,0088:4400,0088:4400,new students3产生的对象,null,null,Student students;students=new Student3

19、;,null,栈内存,数组(Array),数组(Array),堆内存,students,0088:4400,0088:4400,new students3产生的对象,null,null,Student students;students=new Student3;students0=new Student(“lisi”,18);,0088:4660,栈内存,student0标识的Student对象,lisi,18,0088:4660,对数组元素访问,class TestArrays public static void Main()string friendsNames=“Tom”,”Mary

20、”,”Yorck”;Console.WriteLine(“Here are 0 of my friends”,friendsNames.Length);for(int i=0;ifriendsNames.Length;i+)Console.WriteLine(friendsNamesi);Console.ReadKey();,对数组元素访问,语法规则:foreach(in)/can use for each elements,用foreach循环访问数组,对数组元素访问,用foreach循环访问数组,class TestArrays public static void Main()strin

21、g friendsNames=“Tom”,”Mary”,”Yorck”;Console.WriteLine(“Here are 0 of my friends”,friendsNames.Length);foreach(string name in friendsNames)Console.WriteLine(name);,多维数组(矩形数组),C#可以创建多维数组,最常见的多维数组是二维数组(矩形数组),每行元素相同。,格式:type,arrayName;举例:int,num;double,array_double;,多维数组(矩形数组),Class TestArrays public st

22、atic void Main()/Declare a two dimensional array int,array1=new int2,3;/Declare and set array element values int,array2=new int2,3 1,2,3,4,5,6;int,array3=1,2,3,4,5,6;,二维数组的定义和初始化,多维数组(矩形数组),Class TestArrays public static void Main()/Declare and set array element values int,array2=new int2,3 1,2,3,4,

23、5,6;for(int i=0;i array2.GetLength(0);i+)for(int j=0;j array2.GetLength(1);j+)Console.WriteLine(arrayi,j);,二维数组的访问(for循环),多维数组(矩形数组),Class TestArrays public static void Main()/Declare and set array element values int,array2=new int2,3 1,2,3,4,5,6;foreach(int elem in array2)Console.WriteLine(elem);,二

24、维数组的访问(foreach循环),变长数组(数组的数组),Class TestArrays public static void Main()/Declare a jagged array int jaggedArray=new int3;/Set the values of the first array in the jagged array structure jaggedArray0=new int5 1,3,5,7,9;jaggedArray1=new int4 0,2,4,6;jaggedArray2=new int2-1,99;,变长数组的定义和初始化,变长数组(数组的数组),

25、Class TestArrays public static void Main()/Declare and Initalize a jagged array int jaggedArray=new intnew int 1,3,5,7,9;new int 0,2,4,6;new int-1,99;,变长数组的定义和初始化,变长数组(数组的数组),/Declare and Initalize a jagged array int jaggedArray=new intnew int 1,3,5,7,9;new int 0,2,4,6;new int-1,99;for(int i=0;i jag

26、gedArray.Length;i+)for(int j=0 j jaggedArrayi.Length;j+)Console.Write(jaggedArrayij);,变长数组访问(for循环),变长数组(数组的数组),/Declare and Initalize a jagged array int jaggedArray=new intnew int 1,3,5,7,9;new int 0,2,4,6;new int-1,99;foreach(int singleArray in jaggedArray)foreach(int elem in singleArray)Console.Write(elem);,变长数组访问(foreach循环),Array类,C#中,Array类是所有数组的基类Array 类提供能够使用数组的属性和方法。属性:下表介绍一些最常用的Array 类的属性,Array类,Array 类提供能够使用数组的属性和方法。方法:下表介绍一些最常用的Array 类的方法,Thank You!,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号