C#.NET程序设计第6章 集合、索引器实例6-1
设计一个Windows应用程序,定义一个Teacher类,包含姓名和职称两个字段和一个输出自己信息的方法,并用ArrayList实现与实例6-1相同的功能。
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Test6 { public partial class Form2 : Form { ArrayList a = new ArrayList(); public Form2() { InitializeComponent(); } public class Teacher { string name; string job; //copy from iotword.com public Teacher(string name, string job) { this.name = name; this.job = job; } public string getInfo() { return string.Format("姓名:{0},职称:{1}!", name, job); } } private void display() { foreach (object x in a) { Teacher t = (Teacher)x; label4.Text += "\n" + t.getInfo(); } } private void button1_Click(object sender, EventArgs e) { Teacher x = new Teacher(textBox1.Text, textBox2.Text); a.Add(x); label4.Text = ""; display(); } private void button2_Click(object sender, EventArgs e) { label4.Text = ""; display(); } private void button3_Click(object sender, EventArgs e) { int index = Convert.ToInt32(textBox3.Text); Teacher x = new Teacher(textBox1.Text, textBox2.Text); a.Insert(index, x); //copy from iotword.com label4.Text = ""; display(); } private void button4_Click(object sender, EventArgs e) { int index = Convert.ToInt32(textBox3.Text); a.RemoveAt(index); label4.Text = ""; display(); } } }