1、数组的定义
语法规范:
typeSpecifier[] arrayName=new typeSpecifier [numberOfEleements];
(1)数组元素和数组索引
数组在内存中连续使用n个字节数。
数组索引是数组中特定元素的位置。
(2)N-1规则
数组允许的最高索引比数组元素数量少一。
比如:size=myData.Length;
是对数组大小的检测。
示例程序:字母计数
在文本框中输入几个句子,计算每个字母在文本中出现的次数,不需要关注标点符号空格数字和大小写。
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private const int MAXLETTERS = 26;
private const int MAXCHAR = MAXLETTERS - 1;
private const int LETTER = 65;
private Label label1;
private TextBox txtInput;
private Button btnCalc;
private Button btnClose;
private Label label2;
private Label label3;
private ListBox lstOutput;
#region Windows code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtInput = new System.Windows.Forms.TextBox();
this.btnCalc = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.lstOutput = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(38, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(473, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Enter letters";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// txtInput
//
this.txtInput.BackColor = System.Drawing.Color.White;
this.txtInput.Location = new System.Drawing.Point(38, 65);
this.txtInput.Multiline = true;
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(473, 116);
this.txtInput.TabIndex = 1;
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(38, 205);
this.btnCalc.Name = "btnCalc";
this.btnCalc.Size = new System.Drawing.Size(120, 23);
this.btnCalc.TabIndex = 2;
this.btnCalc.Text = "Calculate";
this.btnCalc.UseVisualStyleBackColor = true;
this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(384, 205);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(127, 23);
this.btnClose.TabIndex = 3;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(38, 252);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 23);
this.label2.TabIndex = 4;
this.label2.Text = "Letter";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label3
//
this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;