using System; 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; using System.Collections; namespace c6_5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //使用ArrayList前,要先using 命名空間:System.Collections //using System.Collections; ArrayList name1 = new ArrayList(); List name2 = new List(); string[] name3 = new string[10]; private void button1_Click(object sender, EventArgs e) { //ArrayList特色:ArrayList: //特色1:不特定型別(stirng int,double都可以), //特色2:不固定長度的陣列 //所以很實用 //ArrayList name1 = new ArrayList(); name1.Add(textBox1.Text); //迴圈顯示全部的name1 listBox1.Items.Clear(); foreach(string item in name1) { listBox1.Items.Add(item); } textBox1.Text = ""; } private void button2_Click(object sender, EventArgs e) { //List:特定型別,不固定長度的陣列 //List name2 = new List(); name2.Add(textBox1.Text); //迴圈顯示全部的name1 listBox1.Items.Clear(); foreach (string item in name2) { listBox1.Items.Add(item); } textBox1.Text = ""; } private void button_arraylist_add_Click(object sender, EventArgs e) { //ArrayList特色:ArrayList: //特色1:不特定型別(stirng int,double都可以), //特色2:不固定長度的陣列 //所以很實用 //ArrayList name1 = new ArrayList(); name1.Add(textBox1.Text); //迴圈顯示全部的name1 listBox1.Items.Clear(); foreach (string item in name1) { listBox1.Items.Add(item); } textBox1.Text = ""; } private void button_list_add_Click(object sender, EventArgs e) { //List:特定型別,不固定長度的陣列 //List name2 = new List(); name2.Add(textBox1.Text); //迴圈顯示全部的name1 listBox1.Items.Clear(); foreach (string item in name2) { listBox1.Items.Add(item); } textBox1.Text = ""; } private void button_arry1_add_Click(object sender, EventArgs e) { //傳統陣列,無法不斷新增一個元素,會發生錯誤 //原因:傳統陣列,是固定長度的,所以使用上有限制 //結論:若要動態改變陣列大小,必須用:list陣列,或是Arraylist陣列 //string[] name3 = new string[10]; //label1.Text = name3.Length.ToString(); //name3[name3.Length + 1] = textBox1.Text; } } }