实验四 数组.docx

上传人:b****0 文档编号:9489406 上传时间:2023-05-19 格式:DOCX 页数:15 大小:70.93KB
下载 相关 举报
实验四 数组.docx_第1页
第1页 / 共15页
实验四 数组.docx_第2页
第2页 / 共15页
实验四 数组.docx_第3页
第3页 / 共15页
实验四 数组.docx_第4页
第4页 / 共15页
实验四 数组.docx_第5页
第5页 / 共15页
实验四 数组.docx_第6页
第6页 / 共15页
实验四 数组.docx_第7页
第7页 / 共15页
实验四 数组.docx_第8页
第8页 / 共15页
实验四 数组.docx_第9页
第9页 / 共15页
实验四 数组.docx_第10页
第10页 / 共15页
实验四 数组.docx_第11页
第11页 / 共15页
实验四 数组.docx_第12页
第12页 / 共15页
实验四 数组.docx_第13页
第13页 / 共15页
实验四 数组.docx_第14页
第14页 / 共15页
实验四 数组.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验四 数组.docx

《实验四 数组.docx》由会员分享,可在线阅读,更多相关《实验四 数组.docx(15页珍藏版)》请在冰点文库上搜索。

实验四 数组.docx

实验四数组

实验四数组

【实验目的与要求】

∙掌握一纬数组的定义及使用方法

∙掌握多纬数组的定义及使用方法

∙掌握不规则锯齿数组的定义及其使用方法

∙熟悉数组对象的一些常用属性和静态方法

∙掌握使用数组来求解一些现实问题的步骤和方法

【实验内容与步骤】

   数组是包含若干通过计算所得的索引进行访问的变量的数据结构。

数组中包含的变量(又称数组的元素)具有相同的类型,该类型称为数组的元素类型。

   数组具有秩,它确定与每个数组元素关联的下标的数目。

数组的秩又称为数组的维度。

秩为1的数组称为一维数组,秩大于1的数组称为多维数组。

特定大小的多维数组常根据其大小命名,如二维数组、三维数组,等等。

   数组的每个维度都有一个关联的长度,它是一个大于或等于零的整数。

维度的长度不是数组类型的组成部分,而是在运行时创建数组类型的实例时建立的。

维度的长度确定该维度的下标的有效范围:

对于长度为N的维度,下标范围可以为0到N–1(包括0和N–1)。

数组中的元素总数是数组中各维度长度的积。

如果数组的一个或多个维度的长度为零,则称该数组为空。

   数组的元素类型可以是任意类型,包括数组类型。

一、一纬数组的定义及其使用方法

(1)一纬数组的定义和初始化

同变量一样,数组也必须先定义,后使用。

定义一纬数组的格式如下:

数据类型[]数组名;

∙其中,数据类型可以是C#语言中的各种数据类型,包括数值类型和引用类型。

∙方括号“[]”在这里表示是定义一个数组,而不是普通的一个变量或对象

∙数组名是任意合法的变量标识符

如:

int[]myArray;

初始化一纬数组

静态初始化格式:

数据类型[]数组名={元素1,元素2,……,元素n};

如:

int[]arry={1,2,3,4}

动态初始化:

可以用两条语句来实现:

数据类型[]数组名;//数组定义

数组名=new数据类型[表达式];//动态初始化

或用一条语句来实现:

数据类型[]数组名=new数据类型[表达式];

如:

int[]x=newint[4];

例1:

选择排序法

usingSystem;

namespaceshuzu

{

classselectOrder

{

staticvoidMain(string[]args)

{

inti,j,k,m;

int[]que=newint[]{3,5,34,65,15,74,28,59,122,42};//定义数组并动态初始化

for(i=0;i<10;i++)

{

k=i;

for(j=i+1;j<10;j++)//从i的下一个元素起开始比较

{

if(que[j]

{

k=j;//记录下标值

}

}

if(k!

=i)

{

m=que[i];que[i]=que[k];que[k]=m;

}

}

Console.WriteLine("输出排序后的结果是:

");

for(i=0;i<10;i++)

{

Console.Write("{0}\t",que[i]);

}

}

}

}

例2:

冒泡排序法

usingSystem;

namespaceshuzu

{

classmaopo

{

publicstaticvoidMain()

{

int[]a=newint[10];

Console.WriteLine("请输入10个整数");

for(inti=0;i<10;i++)

{

Console.Write("第[{0}]个数:

",i+1);

a[i]=Int32.Parse(Console.ReadLine());

}

Console.WriteLine("排序前的十个数是:

");

for(inti=0;i<10;i++)

{

Console.Write("{0}\t",a[i]);

}

Console.WriteLine();

inttemp;//临时变量

for(intj=0;j<9;j++)

{

for(inti=0;i<9-j;i++)

{

if(a[i]>a[i+1])

{

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}

}

}

for(inti=0;i<10;i++)

{

Console.WriteLine("排序后的结果是:

");

Console.Write("{0}\t",a[i]);

}

Console.WriteLine();

}

}

}

 

例3:

字符数组

usingSystem;

namespaceshuzu

{

classcharArray

{

publicstaticvoidMain()

{

//定义并初始化name字符数组

char[]name=newchar[]{'G','o','o','d','','m','o','r','n','i','n','g','\0'};

intctr=0;//建立一个计数器ctr,用于遍历数组元素

while(name[ctr]!

=0)//输出数组中的字符,直到遇到一个ASCII码为0的字符为止,结束循环

{

Console.WriteLine("{0}",name[ctr]);

ctr++;

}

}

}

}

 

例4:

对象数组

usingSystem;

usingSystem.Windows.Forms;

namespaceshuzu

{

publicclassuseArray

{

publicstaticvoidMain()

{

ArrayForm[]newfrm=newArrayForm[10];

for(inti=0;i<10;i++)

{

newfrm[i]=newArrayForm("这是第"+i.ToString()+"个窗体");

newfrm[i].ShowDialog();

}

}

}

publicclassArrayForm:

System.Windows.Forms.Form

{

publicArrayForm(stringwhichForm)

{

this.Text=whichForm;

}

}

}

二、多纬数组的定义及其使用

对于多维数组,数组初始值设定项必须具有与数组维数同样多的嵌套级别。

最外面的嵌套级别对应于最左边的维度,而最里面的嵌套级别对应于最右边的维度。

数组各维度的长度是由数组初始值设定项中相应嵌套级别的元素数目确定的。

对于每个嵌套的数组初始值设定项,元素的数目必须与同一级别的其他数组初始值设定项相同。

示例:

int[,]b={{0,1},{2,3},{4,5},{6,7},{8,9}};

创建一个二维数组,其最左边的维度的长度为5,最右边的维度的长度为2:

int[,]b=newint[5,2];

然后用下列值初始化该数组实例:

b[0,0]=0;b[0,1]=1;

b[1,0]=2;b[1,1]=3;

b[2,0]=4;b[2,1]=5;

b[3,0]=6;b[3,1]=7;

b[4,0]=8;b[4,1]=9;

当数组创建表达式同时包含显式维度长度和一个数组初始值设定项时,长度必须是常数表达式,并且各嵌套级别的元素数目必须与相应的维度长度匹配。

示例:

inti=3;

int[]x=newint[3]{0,1,2};//OK

int[]y=newint[i]{0,1,2};//Error,inotaconstant

int[]z=newint[3]{0,1,2,3};//Error,length/initializermismatch

这里,由于维度长度表达式不是常数,因此y的初始值设定项导致编译时错误;另外由于初始值设定项中的长度和元素数目不一致,z的初始值设定项也导致编译时错误。

 

三、锯齿型数组

数组创建表达式允许实例化包含数组类型的元素的数组,但必须手动初始化这类数组的元素。

例如,语句

int[][]a=newint[100][];

创建一个包含100个int[]类型的元素的一维数组。

每个元素的初始值为null。

同样的数组创建表达式不可能也实例化子数组,并且语句

int[][]a=newint[100][5];//Error

导致编译时错误。

实例化子数组必须改为手动执行,如下所示

int[][]a=newint[100][];

for(inti=0;i<100;i++)a[i]=newint[5];

当数组的数组具有“矩形”形状时,即当子数组全都具有相同的长度时,使用多维数组更有效。

在上面的示例中,实例化数组的数组创建101个对象——1个外部数组和100个子数组。

相反,

int[,]=newint[100,5];

只创建单个对象(即一个二维数组)并在单个语句中完成分配。

 

例5:

二维数组及锯齿型数组示例

//Initializingtwo-dimensionalarrays.

usingSystem;

usingSystem.Drawing;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Windows.Forms;

usingSystem.Data;

namespaceTwoDimensionalArrays

{

///

///SummarydescriptionforForm1.

///

publicclassTwoDimensionalArrays:

System.Windows.Forms.Form

{

privateSystem.Windows.Forms.ButtonshowOutputButton;

privateSystem.Windows.Forms.LabeloutputLabel;

///

///Requireddesignervariable.

///

privateSystem.ComponentModel.Containercomponents=null;

publicTwoDimensionalArrays()

{

//

//RequiredforWindowsFormDesignersupport

//

InitializeComponent();

//

//TODO:

AddanyconstructorcodeafterInitializeComponentcall

//

}

///

///Cleanupanyresourcesbeingused.

///

protectedoverridevoidDispose(booldisposing)

{

if(disposing)

{

if(components!

=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#regionWindowsFormDesignergeneratedcode

///

///RequiredmethodforDesignersupport-donotmodify

///thecontentsofthismethodwiththecodeeditor.

///

privatevoidInitializeComponent()

{

this.showOutputButton=newSystem.Windows.Forms.Button();

this.outputLabel=newSystem.Windows.Forms.Label();

this.SuspendLayout();

//

//showOutputButton

//

this.showOutputButton.Location=newSystem.Drawing.Point(102,8);

this.showOutputButton.Name="showOutputButton";

this.showOutputButton.Size=newSystem.Drawing.Size(88,23);

this.showOutputButton.TabIndex=0;

this.showOutputButton.Text="ShowOutput";

this.showOutputButton.Click+=newSystem.EventHandler(this.showOutputButton_Click);

//

//outputLabel

//

this.outputLabel.Location=newSystem.Drawing.Point(8,48);

this.outputLabel.Name="outputLabel";

this.outputLabel.Size=newSystem.Drawing.Size(280,104);

this.outputLabel.TabIndex=1;

//

//TwoDimensionalArrays

//

this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13);

this.ClientSize=newSystem.Drawing.Size(292,165);

this.Controls.AddRange(newSystem.Windows.Forms.Control[]{

this.outputLabel,

this.showOutputButton});

this.Name="TwoDimensionalArrays";

this.Text="TwoDimensionalArrays";

this.ResumeLayout(false);

}

#endregion

///

///Themainentrypointfortheapplication.

///

[STAThread]

staticvoidMain()

{

Application.Run(newTwoDimensionalArrays());

}

privatevoidshowOutputButton_Click(objectsender,

System.EventArgse)

{

//declarationandinitializationofrectangulararray

int[,]array1=newint[,]{{1,2,3},{4,5,6}};

//declarationandinitializationofjaggedarray

int[][]array2=newint[3][];

array2[0]=newint[]{1,2};

array2[1]=newint[]{3};

array2[2]=newint[]{4,5,6};

outputLabel.Text="Valuesinarray1byroware\n";

//outputvaluesinarray1

for(inti=0;i

{

for(intj=0;j

(1);j++)

outputLabel.Text+=array1[i,j]+"";

outputLabel.Text+="\n";

}

outputLabel.Text+="\nValuesinarray2byroware\n";

//outputvaluesinarray2

for(inti=0;i

{

for(intj=0;j

outputLabel.Text+=array2[i][j]+"";

outputLabel.Text+="\n";

}

}//endmethodshowOutputButton_Click

}

}

四、数组类(Array)的常用属性和方法的使用

Array类是支持数组的语言实现的基类。

但是,只有系统和编译器能够从Array类显式派生。

用户应当使用语言提供的数组构造。

一个元素就是Array中的一个值。

Array的长度是它可包含的元素总数。

Array的秩是Array中的维数。

Array中维度的下限是Array中该维度的起始索引,多维Array的各个维度可以有不同的界限。

Type对象提供有关数组类型声明的信息。

具有相同数组类型的Array对象共享同一Type对象。

Type.IsArray和Type.GetElementType可能不返回所预期的Array形式的结果,因为如果某个数组被强制转换为Array类型,则结果是对象,而非数组。

即,typeof(System.Array).IsArray返回false,而typeof(System.Array).GetElementType返回空引用。

与大多数类不同,Array提供CreateInstance方法,以便允许晚期绑定访问,而不是提供公共构造函数。

Array.Copy方法不仅可在同一类型的数组之间复制元素,而且可在不同类型的标准数组之间复制元素;它会自动处理强制类型转换。

例6:

使用数组类的属性和方法

usingSystem;

publicclassSamplesArray{

publicstaticvoidMain(){

//CreatesandinitializesanewintegerarrayandanewObjectarray.

int[]myIntArray=newint[5]{1,2,3,4,5};

Object[]myObjArray=newObject[5]{26,27,28,29,30};

//Printstheinitialvaluesofbotharrays.

Console.WriteLine("Initially,");

Console.Write("integerarray:

");

PrintValues(myIntArray);

Console.Write("Objectarray:

");

PrintValues(myObjArray);

//CopiesthefirsttwoelementsfromtheintegerarraytotheObjectarray.

Array.Copy(myIntArray,myObjArray,2);

//Printsthevaluesofthemodifiedarrays.

Console.WriteLine("\nAftercopyingthefirsttwoelementsoftheintegerarraytotheObjectarray,");

Console.Write("integerarray:

");

PrintValues(myIntArray);

Console.Write("Objectarray:

");

PrintValues(myObjArray);

//CopiesthelasttwoelementsfromtheObjectarraytotheintegerarray.

Array.Copy(myObjArray,myObjArray.GetUpperBound(0)-1,myIntArray,myIntArray.GetUpperBound(0)-1,2);

//Printsthevaluesofthemodifiedarrays.

Console.WriteLine("\nAftercopyingthelasttwoelementsoftheObjectarraytotheintegerarray,");

Console.Write("integerarray:

");

PrintValues(myIntArray);

Console.Write("Objectarray:

");

PrintValues(myObjArray);

}

publicstaticvoidPrintValues(Object[]myArr){

foreach(ObjectiinmyArr){

Console.Write("\t{0}",i);

}

Console.WriteLine();

}

publicstaticvoidPrintValues(int[]myArr){

foreach(intiinmyArr){

Console.Write("\t{0}",i);

}

Console.WriteLine();

}

}

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

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2