C#——利用ArrayList进行学生集合的增删插入和遍历操作

首先设计如下界面;

C#——利用ArrayList进行学生集合的增删插入和遍历操作

编写如下代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;//引入包
namespace 动态数组
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label3.Text = "";
        }
        ArrayList arr = new ArrayList();
        public void display() 
        {
            foreach(object x in arr)
            {
                Student s = (Student)x;
                label3.Text += "\n"+s.message();
            }
        }
        //添加到末尾
        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1 .Text !=""&&textBox2 .Text !="")
            {
                Student x = new Student(textBox1.Text, textBox2.Text);
                arr.Add(x);
                label3.Text = "";
                display();
                textBox1.Text = "";
                textBox2.Text = "";
            }else
            {
                MessageBox.Show("请输入需要添加的学生信息!");
            }
            
        }
        //遍历
        private void button3_Click(object sender, EventArgs e)
        {
            if (arr.Count != 0)
            {
                label3.Text = "";
                display();
            }
            else
            {
                MessageBox.Show("学生信息为空,请添加学生信息后遍历");
            }
        }
        //插入到
        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox1 .Text !=""&&textBox2 .Text !=""&&textBox3 .Text !="")
            {
                arr.Insert(Convert.ToInt32(textBox3.Text), new Student(textBox1.Text, textBox2.Text));
                label3.Text = "";
                display();
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }else
            {
                MessageBox.Show("请输入需要插入的信息和位置!");
            }

        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if(arr.Count !=0)
            {
                if (textBox3.Text != "")
                {
                    arr.RemoveAt(Convert.ToInt32(textBox3.Text));
                    label3.Text = "";
                    display();
                    textBox3.Text = "";
                }
                else 
                {
                    MessageBox.Show("请输入需要删除的信息");
                }
            }else
            {
                MessageBox.Show("没有可删除的学生信息");
            }

        }
    }

    //学生类
    public class Student
    {
        private string sto;
        private string name;
        public Student(string sto,string name)
        {
            this.sto = sto;
            this.name = name;
        }
        public string message() 
        {
            return "学号:" + sto + "\n姓名;" + name ;
        }
    }
}
 

运行结果:

C#——利用ArrayList进行学生集合的增删插入和遍历操作