nullnull第03章
数组、结构和枚举
null内容提要:
数组、结构和枚举的概念
一维数组、二维数组的定义及使用
结构和枚举的定义及使用
目的要求:
理解数组的含义
掌握一维数组、二维数组的用法
掌握结构和枚举的定义及使用
重点难点:
一维数组、二维数组和结构简介 int score1;
System.Console.ReadLine(score1)
int score2;
System.Console.ReadLine(score2)
int score3;
System.Console.ReadLine(score3)
int score4;
System.Console.ReadLine(score4)
int score5;
System.Console.ReadLine(score5)
int score6;
System.Console.ReadLine(score6)
int score7;
System.Console.ReadLine(score7)第七位学生的分数第六位学生的分数第五位学生的分数第四位学生的分数第三位学生的分数第二位学生的分数第一位学生的分数score [6]score [5]score [4]score [3]score [1]score [0]score [2]简介 应用程序数 组存储学员的分数int score[ ] = new int[7];在数组的术语中,元素表示数组中存储的值,数组长度指数组中存储的值的总数,数组秩指数组的总维数null2.1 数组
2.1.1 数组的概念
数组是一种数据结构,它包含大量相同类型的变量,这些变量可以通过一个数组名和数组下标(或者叫索引),来访问。包含在数组中的变量,也称为数组元素。
在C#中,数组有一维(只有一个下标)或者多维(有多个下标)。
对于每一维中,数组中数组元素的个数叫这个维的数组长度。无论是一维数组还是多维数组,每个维的下标都是从0开始,结束于这个维的数组长度减1。 null2.1.2 一维数组
1.一维数组的定义
格式: 数组类型[ ] 数组名;
例:int[ ] a;
string[ ] arr;
char[ ] carr;
null2.1.2 一维数组
2.动态初始化
动态初始化需要借助new运算符,为数组元素分配内存空间,并为数组元素赋初值,数值类型初始化为0,布尔类型初始化为false,字符串类型初始化为null(空值)。
格式: 数组名=new 数据类型[数组长度];
例:a =new int[6];
也可以将定义和动态初始化写在一起:
例: string[ ] arr=new string[3 ]{“as”,”vb”,”23”};
nullusing System;
class zy4
{
static void Main()
{
bool[] a=new bool[2];
char[] b=new char[3];
double[] c=new double[4];
string[] d=new string[5];
Console.WriteLine(a[0])//flase;
Console.WriteLine(b[0]//null);
Console.WriteLine(c[0]//0);
Console.WriteLine(d[0]//null);
}
}null2.1.2 一维数组
3.静态初始化
静态初始化数组的格式如下:
数据类型[ ] 数组名={元素1[,元素2...]};
用这种
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
对数组进行初始化时,无须
说明
关于失联党员情况说明岗位说明总经理岗位说明书会计岗位说明书行政主管岗位说明书
数组元素的个数,只需按顺序列出数组中的全部元素即可,系统会自动计算并分配数组所需的内存空间。
例如:
int[ ] IntArr={-45,9,29,32,46};
char[ ] StringArr={‘a’,’b’, ‘c’, ‘d’};null
5.访问一维数组中的元素
在C#中是通过数组名和下标来访问数组元素的。
例:int[ ] a=new int[12];
int c=5;
int b=6;
a[b+c]+=2;
每一个C#的数组都知道自己的长度,数组的长度由 Length 决定。
int i=a.Length; //i=12
null
5.访问一维数组中的元素
例:计算a数组中前3个元素的总和,并且把结果保存到变量sum中。
int sum=a[0]+a[1]+a[2];
如果要把数组中的元素全部相加,则用循环比较方便。
例: for(int i=0;i
=0;j--)
Console.Write (“{0}\0”, nums[j]);
Console.WriteLine();
}
} 运行结果如下:null using System;
class Rnd_36_7
{
static void Main( )
{
int[] a=new int[7];
Random ran=new Random();
for(int i=0;iy)
return x;
else
return y;
}
public void Main()
{
Console.WriteLine(“the max of 6 and 8 is:{0}”,max(6,8));
}
}
程序的输出是
the max of 6 and 8 is:82.1.6 方法中的参数 2.1.6 方法中的参数 C#中方法的参数有四种类型,它们分别是:
值参数
值参数不含任何修饰符
引用参数
以ref修饰符声明引用型参数
输出参数
以out修饰符声明输出参数
数组参数。
以params修饰符声明数组型参数 值参数值参数当利用值向方法传递参数时,编译程序给实参的值做一份拷贝,并且将此拷贝传递给该方法。被调用的方法不会修改内存中实参的值,所以使用值参数时,可以保证实际值是安全的。 5.3.1值参数5.3.1值参数下面这个方法试图完成将两个数值进行交换:
static void Swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}5.3.1值参数5.3.1值参数下面我们调用该函数完成两个数的交换:
static void Main()
{
int i = 1, j = 2;
Swap(i, j);
Console.WriteLine("i = {0}, j = {1}", i, j);
}
上面调用的输出结果是:
i = 1, j = 2 5.3.1值参数5.3.1值参数分析:
出现这种结果的原因是,Swap方法的参数是使用的值传递方式,上面我们强调了值参数在传递时,编译器会生成一份拷贝,所以Swap交换的实际上是拷贝的值,而不是实参的值 引用参数 引用参数 从上面例子中,我们知道值参数无法完成数值交换,如果要完成数据交换,必须在方法调用传入实参本身,而非实参的拷贝。C#为我们提供了另外一种参数传递方式,这就是引用参数,和值参不同的是引用型参数并不开辟新的内存区域,当利用引用型参数向方法传递形参时,编译程序将把实际值在内存中的地址传递给方法。 5.3.2 引用参数5.3.2 引用参数下面我们还是以交换两个数值的例子来讲解引用参数的使用。
static void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}5.3.2 引用参数5.3.2 引用参数因此我们在调用Swap时需要如下调用:
static void Main()
{
int i = 1, j = 2;
Swap(ref i, ref j);
Console.WriteLine("i = {0}, j = {1}", i, j);
}
这次函数输出结果为:
i = 2, j = 1输出参数 输出参数 与引用型参数类似,输出型参数也不开辟新的内存区域。与引用型参数的差别在于:
调用方法前无需对变量进行初始化。
输出型参数用于传递方法返回的数据。
在调用含有引用参数的方法,需要在传递参数时,使用out关键字来声明该参数为输出参数。在方法返回后,传递的变量被认为经过了初始化。 输出参数输出参数下面我们就引用参数和输出参数,举例来做一对比。
static void SquareRef( ref int x )
{
x = x * x;
}
static void SquareOut( out int x )
{
x = 8;
x = x * x;
}输出参数输出参数static void Main()
{
int i = 3, j ;
SquareRef(ref i);
SquareOut(out j);
Console.WriteLine("i = {0}, j = {1}", i, j);
}
函数输出结果为:
i = 9, j = 64输出参数输出参数分析
从上面这个例子中,我们可以看出引用参数和输出参数之间的差别。SquareRef方法采用了引用参数方式,而SquareOut方法采用了输出参数方法,这两个方法所做的事情完全相同,都是进行平方运算。只是在SquareOut方法中把x的值初始化为8。 数组参数 数组参数 数组也可以作为方法的参数,这是用来表示参数称为数组参数,数组参数只能放在参数列表的最后,并且数组参数只能是一维数组。
声明一个数组参数需要使用params关键字 数组参数数组参数using System;
class Test
{
static int Sum(params int[] args)
{
int s=0;
foreach(int n in args)
{
s+=n;
}
return s;
}
static void Main()
{
int[] var=new int[]{1,2,3,4,5};
Console.WriteLine("The Sum:"+Sum(var));
Console.WriteLine("The Sum:" + Sum(10, 20, 30, 40, 50));
}
}
程序经编译后执行输出:
The Sum:15 The Sum:150null
结构是用户自定义的数据类型。结构类型是指把各种不同类型数据信息组合在一起形成的组合类型。
2. 2 结构结构struct structEx
{
public int structDataMember;
public void structMethod1()
{
//structMethod1 实现
}
}自定义数据类型
可以在其内部定义方法
无法实现继承
属于值类型
主要结构:struct student
{
public int stud_id;
public string stud_name;
public float stud_marks;
public void show_details()
{
//显示学生详细信息
}
}
结构数据成员方法所有与 Student 关联的详细信息都可以作为一个整体进行存储和访问null
1、结构的声明
结构类型也是先声明后使用。
声明结构类型时要使用struct关键字,声明结构类型的一般语法格式如下:
struct 标识符
{
//结构成员定义
}null例:定义一个表示矩形的结构。
struct Rectangle //定义名为Rectangle的结构
{
public double 长;
public double 宽;
}
public代表这个成员是公有成员,可以被外界访问。null 结构体中也可以放方法成员,如可以在结构体中计算面积。
例: struct Rectangle //定义名为Student的结构
{ public double 长;
public double 宽;
public void 面积( )
{
Console.WriteLine(“面积={0}”,长*宽);
}
}
null2、 结构成员的访问
结构成员可分为两类,一是实例成员,一是静态成员。
若成员名前有static关键字,则该成员为静态成员,否则为实例成员。静态成员通过结构名来访问,格式:
结构名.静态成员名
实例成员的访问是通过创建结构类型的变量来访问结构的实例成员。格式:
结构名 标识符;
实例成员访问格式为:
结构变量名.实例成员名null练习:
1. 声明结构类型的关键字为( )
A. class
B. double
C. struct
D. int[ ]Dnullusing System;
using System.Collections.Generic;
using System.Text;
class Program
{
struct s
{
public int num;
public string name;
public char sex;
public int age;
public float score;
}
static void Main()
{
s c1;
c1.num=1001;
c1.name="zhang";
c1.sex='M';
c1.age=19;
c1.score=88;
Console.WriteLine(c1.num);
Console.WriteLine(c1.name);
}
}null 枚举类型是用户自定义的数据类型,是一种允许用符号代表数据的值类型。
枚举是指程序中某个变量具有一组确定的值,通过“枚举”可以将其值一一列出来。
例如:将一个星期的7天分别用符号Monday、Tuesday、Wednesday、Thursday、Friday、Saturday和Sunday来表示。2. 3 枚举null
1. 枚举类型的定义
枚举类型是一种用户自己定义的由一组指定常量集合组成的独特类型。
格式:
enum 枚举名
{枚举成员表}[;]
null例:
enum WeekDay
{Sun, Mon, Tue, Wed, Thu, Fri, Sat};
注意:两个枚举成员名不能完全相同。
枚举枚举public class ColorTest
{
public enum Color
{
Red,
Blue,
Green,
Black,
Yellow
}
public void SetColor (Color c)
{
//处理颜色
}
static void Main()
{
ColorTest c = new ColorTest();
c.SetColor (ColorTest.Color.Red);
}
}枚举(Enum,Enumerator 的缩写)是一组已命名的数值常量
用于定义具有一组特定值的数据类型
枚举以 enum 关键字声明Red = 0枚举(续)枚举(续)C# 中的枚举包含与值关联的数字
默认情况下,将 0 值赋给枚举的第一个元素,然后对每个后续的枚举元素按 1 递增
在初始化过程中可重写默认值public enum Color
{
Red=1,
Blue=2,
Green=3,
Black=4,
Yellow=5
}null2. 枚举成员的赋值
在定义的枚举类型中,每一个枚举成员都有一个相对应的整数值,对于枚举成员对应的整数值,默认情况下C#规定第1个枚举成员的值取0,它后面的每一个枚举成员的值按加上1递增。
例: enum WeekDay
{Sun, Mon, Tue, Wed, Thu, Fri, Sat}; 中,其枚举成员Sun、Mon、Tue、Wed、Thu、Fri和Sat在执行程序时,分别被赋予整数值0、1、2、3、4、5和6。
null例:输出枚举成员对应的整数值。
using System;
enum color
{ yellow, brown, blue, black, purple }
class EnumDemo
{
static void Main( )
{
Console.WriteLine(“yellow={0}”, color.yellow);
Console.WriteLine(“yellow={0}”, (int)color.yellow);
Console.WriteLine(“brown={0}”, (int)color.brown);
Console.WriteLine(“blue={0}”, (int)color.blue);
Console.WriteLine(“black={0}”, (int)color.black);
Console.WriteLine(“purple={0}”, (int)color.purple);
}
}注意:枚举成员的值在不经过显式转换前,是不会变换成整数值的。null(1)为第1个枚举成员赋值
在定义枚举类型时,为第1个枚举成员赋值,它后面的每一个枚举成员的值按加1递增。
例:
using System;
enum color
{ yellow=-1, brown, blue, black, purple }
class EnumDemo
{ static void Main( )
{
Console.WriteLine(“yellow={0}”, color.yellow);
Console.WriteLine(“yellow={0}”, (int)color.yellow);
Console.WriteLine(“brown={0}”, (int)color.brown);
Console.WriteLine(“blue={0}”, (int)color.blue);
Console.WriteLine(“black={0}”, (int)color.black);
Console.WriteLine(“purple={0}”, (int)color.purple);
}
}null(2)为某一个枚举成员赋值
如果在定义枚举类型时,直接为某个枚举成员赋值,则其他枚举成员依次取值,如:
enum color
{ yellow, brown, blue, black=6, purple};
null
例:
using System;
enum color
{ yellow, brown, blue, black=6, purple }
class EnumDemo
{
static void Main( )
{
Console.WriteLine(“yellow={0}”, color.yellow);
Console.WriteLine(“yellow={0}”, (int)color.yellow);
Console.WriteLine(“brown={0}”, (int)color.brown);
Console.WriteLine(“blue={0}”, (int)color.blue);
Console.WriteLine(“black={0}”, (int)color.black);
Console.WriteLine(“purple={0}”, (int)color.purple);
}
}null
(3)为多个枚举成员赋值
在定义枚举类型时,还可以为多个枚举成员赋值。
例如:
enum color
{ yellow, brown=3, blue, black=-3, purple};
null
例:输出枚举成员对应的整数值。
using System;
enum color
{ yellow, brown=3, blue, black=-3, purple}
class EnumDemo
{ static void Main( )
{
Console.WriteLine(“yellow={0}”, color.yellow);
Console.WriteLine(“yellow={0}”, (int)color.yellow);
Console.WriteLine(“brown={0}”, (int)color.brown);
Console.WriteLine(“blue={0}”, (int)color.blue);
Console.WriteLine(“black={0}”, (int)color.black);
Console.WriteLine(“purple={0}”, (int)color.purple);
}
}null
(4)为多个枚举成员赋同样的值
在定义枚举类型时,可以让多个枚举成员具有同样的整数值,如:
enum color
{ yellow, brown=3, blue, black= blue, purple};
//这里black的整数值就为4null
例:输出枚举成员对应的整数值。
using System;
class EnumDemo
{
enum color
{yellow, brown=3, blue, black= blue, purple}
static void Main( )
{
Console.WriteLine(“yellow={0}”, color.yellow);
Console.WriteLine(“yellow={0}”, (int)color.yellow);
Console.WriteLine(“brown={0}”, (int)color.brown);
Console.WriteLine(“blue={0}”, (int)color.blue);
Console.WriteLine(“black={0}”, (int)color.black);
Console.WriteLine(“purple={0}”, (int)color.purple);
}
}null3. 枚举成员的访问
可以通过枚举型变量和枚举名两种方式来访问枚举成员。
(1)通过变量访问枚举成员
先要声明一个枚举型变量,形式如下:
枚举类型名 变量名;
例:
enum WeekDay
{ Sun, Mon, Tue, Wed, Thu, Fri, Sat };
Weekday wd1; //声明一个枚举型变量wd1
wd1=WeekDay.Sun; //给枚举型变量wd1赋值null例:声明一个枚举类型,通过枚举型变量访问枚举成员。
enum Color
{ Red,Green=10,Blue}
class Test
{ static void Main()
{ Console.Write(“请输入c的值:");
int c=int.Parse(Console.ReadLine()); //输入10
Color b=(Color)c;
Console.WriteLine(b);
switch(b)
{ case Color.Red:
Console.WriteLine("Red="+(int)b); break;
case Color.Green:
Console.WriteLine("Green="+(int)b); break;
case Color.Blue:
Console.WriteLine("Blue="+(int)b); break;
default:
Console.WriteLine("Invalid color"); break;
} }}null(2)通过枚举名访问枚举成员
通过枚举名访问枚举成员的一般形式:
枚举名.枚举成员;null
练习:已知
enum Course
{Math, English, Computer, History, Chemistry=0, Philology, Philosophy}
Course cc;
则下列语句错误的是( )
A.int a=(int)Course.English;
B.cc=Course.English+2;
C.cc=5;
D.bool b=Course.Math==Course.Chemistry;