C#ArrayList

"TestArrayList.cs"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ListTest
{
    class TestArrayList
    {
        public static ArrayList array;
    }
}

"Form1.cs"
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.IO;

namespace ListTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestArrayList.array = new System.Collections.ArrayList();
            this.button1.Enabled = false;
            this.button2.Enabled = true;
            this.textBox3.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TestArrayList.array.Add(this.textBox3.Text);
            this.textBox1.Text = (TestArrayList.array.Capacity).ToString();
            this.textBox2.Text = (TestArrayList.array.Count).ToString();
            this.button3.Enabled = true;
            this.button4.Enabled = true;
            this.button5.Enabled = true;
            this.button7.Enabled = true;
            //this.textBox3.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int size = TestArrayList.array.Count;
            int index = TestArrayList.array.IndexOf(this.textBox3.Text);
            if (index== 0)
            {
                this.textBox3.Text = TestArrayList.array[size-1].ToString();
            }
            else
            {
                this.textBox3.Text = TestArrayList.array[index-1].ToString();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.button2.Enabled = false;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
            this.button6.Enabled = false;
            this.button7.Enabled = false;
            this.textBox3.Enabled = false;
            MaximumSize = new Size(350, 264);
            MinimumSize = new Size(350, 264);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int size = TestArrayList.array.Count;
            int index = TestArrayList.array.IndexOf(this.textBox3.Text);
            if (index == size - 1)
            {
                this.textBox3.Text = TestArrayList.array[0].ToString();
            }
            else
            {
                this.textBox3.Text = TestArrayList.array[index + 1].ToString();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int index = TestArrayList.array.IndexOf(this.textBox3.Text);
            int size = TestArrayList.array.Count;
            if (index == -1)
            {
                MessageBox.Show(this.textBox3.Text + "not exit!");
            }
            else
            {
                if (index == size - 1)
                {
                    this.textBox3.Text = TestArrayList.array[0].ToString();
                }
                else
                {
                    this.textBox3.Text = TestArrayList.array[index + 1].ToString();
                }
                TestArrayList.array.Remove(TestArrayList.array[index]);
                this.textBox1.Text = (TestArrayList.array.Capacity).ToString();
                this.textBox2.Text = (TestArrayList.array.Count).ToString();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.button6.Enabled = true;
            StreamWriter sw = null;
            sw = new StreamWriter("./hello.txt", true, System.Text.Encoding.UTF8);
            foreach (string s in TestArrayList.array)
            {
                sw.WriteLine(s);
            }
            sw.Flush();
            sw.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.button3.Enabled = true;
            this.button4.Enabled = true;
            this.button7.Enabled = true;
            StreamReader sr = null;
            sr = new StreamReader("./hello.txt", System.Text.Encoding.UTF8);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);//将文件指针设定到文件开头
            string str = sr.ReadLine();//从流中读入一行字符
            while (str != null)
            {
                TestArrayList.array.Add(str);
                str = sr.ReadLine();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值