📄 frmarraylistdemo.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace HashTable
{
/// <summary>
/// Summary description for Form1.Form1 的摘要说明。
/// </summary>
public class frmArrayListDemo : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Label lblInfo;
private System.Windows.Forms.Button btnRemove;
private System.Windows.Forms.Button btnFirst;
private System.Windows.Forms.Button btnLast;
private System.Windows.Forms.Button btnIsEmpty;
private System.Windows.Forms.Button btnContains;
private System.Windows.Forms.Button btnLocation;
private System.Windows.Forms.Button btnTrim;
private System.Windows.Forms.Button btnDisplay;
private System.Windows.Forms.TextBox txtDisplay;
private System.Windows.Forms.Button btnSort;
private System.Windows.Forms.Button btnSearch;
/// <summary>
/// Required designer variable.必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmArrayListDemo()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtInput = new System.Windows.Forms.TextBox();
this.lblInfo = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnRemove = new System.Windows.Forms.Button();
this.btnFirst = new System.Windows.Forms.Button();
this.btnLast = new System.Windows.Forms.Button();
this.btnIsEmpty = new System.Windows.Forms.Button();
this.btnContains = new System.Windows.Forms.Button();
this.btnLocation = new System.Windows.Forms.Button();
this.btnTrim = new System.Windows.Forms.Button();
this.btnDisplay = new System.Windows.Forms.Button();
this.txtDisplay = new System.Windows.Forms.TextBox();
this.btnSort = new System.Windows.Forms.Button();
this.btnSearch = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(163, 26);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(120, 21);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
//
// lblInfo
//
this.lblInfo.Location = new System.Drawing.Point(10, 28);
this.lblInfo.Name = "lblInfo";
this.lblInfo.Size = new System.Drawing.Size(120, 24);
this.lblInfo.TabIndex = 1;
this.lblInfo.Text = "输入字符串:";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(10, 78);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(90, 24);
this.btnAdd.TabIndex = 2;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(134, 78);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(90, 24);
this.btnRemove.TabIndex = 3;
this.btnRemove.Text = "删除(&R)";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// btnFirst
//
this.btnFirst.Location = new System.Drawing.Point(269, 78);
this.btnFirst.Name = "btnFirst";
this.btnFirst.Size = new System.Drawing.Size(90, 24);
this.btnFirst.TabIndex = 4;
this.btnFirst.Text = "第一个(&F)";
this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
//
// btnLast
//
this.btnLast.Location = new System.Drawing.Point(10, 129);
this.btnLast.Name = "btnLast";
this.btnLast.Size = new System.Drawing.Size(90, 25);
this.btnLast.TabIndex = 5;
this.btnLast.Text = "最后一个(&L)";
this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
//
// btnIsEmpty
//
this.btnIsEmpty.Location = new System.Drawing.Point(134, 129);
this.btnIsEmpty.Name = "btnIsEmpty";
this.btnIsEmpty.Size = new System.Drawing.Size(90, 25);
this.btnIsEmpty.TabIndex = 6;
this.btnIsEmpty.Text = "为空?(&E)";
this.btnIsEmpty.Click += new System.EventHandler(this.btnIsEmpty_Click);
//
// btnContains
//
this.btnContains.Location = new System.Drawing.Point(269, 129);
this.btnContains.Name = "btnContains";
this.btnContains.Size = new System.Drawing.Size(90, 25);
this.btnContains.TabIndex = 7;
this.btnContains.Text = "包含(&C)";
this.btnContains.Click += new System.EventHandler(this.btnContains_Click);
//
// btnLocation
//
this.btnLocation.Location = new System.Drawing.Point(10, 181);
this.btnLocation.Name = "btnLocation";
this.btnLocation.Size = new System.Drawing.Size(90, 25);
this.btnLocation.TabIndex = 8;
this.btnLocation.Text = "位置(&O)";
this.btnLocation.Click += new System.EventHandler(this.btnLocation_Click);
//
// btnTrim
//
this.btnTrim.Location = new System.Drawing.Point(134, 181);
this.btnTrim.Name = "btnTrim";
this.btnTrim.Size = new System.Drawing.Size(90, 25);
this.btnTrim.TabIndex = 9;
this.btnTrim.Text = "整理(&T)";
this.btnTrim.Click += new System.EventHandler(this.btnTrim_Click);
//
// btnDisplay
//
this.btnDisplay.Location = new System.Drawing.Point(269, 181);
this.btnDisplay.Name = "btnDisplay";
this.btnDisplay.Size = new System.Drawing.Size(90, 25);
this.btnDisplay.TabIndex = 10;
this.btnDisplay.Text = "显示(&D)";
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
//
// txtDisplay
//
this.txtDisplay.Enabled = false;
this.txtDisplay.Location = new System.Drawing.Point(10, 233);
this.txtDisplay.Multiline = true;
this.txtDisplay.Name = "txtDisplay";
this.txtDisplay.Size = new System.Drawing.Size(345, 267);
this.txtDisplay.TabIndex = 11;
this.txtDisplay.Text = "";
//
// btnSort
//
this.btnSort.Location = new System.Drawing.Point(394, 78);
this.btnSort.Name = "btnSort";
this.btnSort.Size = new System.Drawing.Size(90, 24);
this.btnSort.TabIndex = 12;
this.btnSort.Text = "排序(&S)";
this.btnSort.Click += new System.EventHandler(this.btnSort_Click);
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(394, 129);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(90, 25);
this.btnSearch.TabIndex = 13;
this.btnSearch.Text = "搜索(&H)";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// frmArrayListDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(499, 513);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.btnSort);
this.Controls.Add(this.txtDisplay);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.btnDisplay);
this.Controls.Add(this.btnTrim);
this.Controls.Add(this.btnLocation);
this.Controls.Add(this.btnContains);
this.Controls.Add(this.btnIsEmpty);
this.Controls.Add(this.btnLast);
this.Controls.Add(this.btnFirst);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.lblInfo);
this.Name = "frmArrayListDemo";
this.Text = "数组列表演示";
this.ResumeLayout(false);
}
#endregion
// Declaring a arraylist having capacity 1
ArrayList arrList = new ArrayList(1);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmArrayListDemo());
}
// Add item to the end of arraylist
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(txtInput.Text!= string.Empty)
{
arrList.Add( txtInput.Text );
txtDisplay.Text = "添加到末尾:" + txtInput.Text;
txtInput.Clear();
}
else
MessageBox.Show("输入一个有效字符串");
}
// Remove specified item from arrList
private void btnRemove_Click(object sender, System.EventArgs e)
{
if(txtInput.Text!= string.Empty)
{
arrList.Remove( txtInput.Text );
txtDisplay.Text = "已删除: " + txtInput.Text;
txtInput.Clear();
}
else
MessageBox.Show("输入一个有效字符串");
}
// Display first element
private void btnFirst_Click(object sender, System.EventArgs e)
{
// Get first element
try
{
txtDisplay.Text = "第一个元素: " + arrList[0];
}
catch ( ArgumentOutOfRangeException outOfRange )
{
txtDisplay.Text = outOfRange.ToString();
MessageBox.Show("数组列表为空");
}
}
// Display last element
private void btnLast_Click(object sender, System.EventArgs e)
{
// get last element
try
{
txtDisplay.Text = "最后一个元素:" +
arrList[ arrList.Count - 1 ];
}
// show exception if no elements in arrrayList
catch ( ArgumentOutOfRangeException outOfRange )
{
txtDisplay.Text = outOfRange.ToString();
MessageBox.Show("数组列表为空");
}
}
// determine whether arrList is empty
private void btnIsEmpty_Click(object sender, System.EventArgs e)
{
txtDisplay.Text = ( arrList.Count == 0 ?
"数组列表为空" : "数组列表不为空" );
}
// Determines whether arrList contains specified object
private void btnContains_Click(object sender, System.EventArgs e)
{
if(txtInput.Text!= string.Empty)
{
if ( arrList.Contains( txtInput.Text ) )
txtDisplay.Text = "数组列表包含" + txtInput.Text;
else
txtDisplay.Text = txtInput.Text + "没有找到";
}
else
MessageBox.Show("输入一个有效字符串");
}
// Determine location of specified object
private void btnLocation_Click(object sender, System.EventArgs e)
{
if ( arrList.Contains( txtInput.Text ) )
txtDisplay.Text = "元素位于 " + arrList.IndexOf( txtInput.Text );
else
txtDisplay.Text = txtInput.Text + "没有找到";
}
// Trim array list to current size
private void btnTrim_Click(object sender, System.EventArgs e)
{
arrList.TrimToSize();
txtDisplay.Text = "将向量大小整理";
}
private void btnDisplay_Click(object sender, System.EventArgs e)
{
if(arrList.Count >0)
{
this.txtDisplay.Text = "数组列表中的元素:" ;
for(int i=0;i<arrList.Count;i++)
{
txtDisplay.Text += " " + arrList[i].ToString();
}
}
else
txtDisplay.Text = "数组列表为空";
}
private void btnSort_Click(object sender, System.EventArgs e)
{
arrList.Sort(0,arrList.Count,null);
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
if(arrList.Contains(txtInput.Text))
{
int elementIndex= arrList.BinarySearch(this.txtInput.Text);
txtDisplay.Text = "在 arrList 中找到元素- 索引是:" + elementIndex.ToString();
}
else
txtDisplay.Text = "在 arrList 中未找到元素";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -