using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; // 加入才看得到ArrayList
namespace ConsoleArrayList
{
class ArrayListTest
{
static void Main(string[] args)
{
ArrayList custList = new ArrayList();
object custName;
int k, count;
string goOn = "Y";
while (goOn.ToUpper() == "Y")
{
Console.Write("請輸入客戶名稱: ");
custName = Console.ReadLine();
custList.Add(custName);
Console.Write("是否繼續輸入? Y/N");
goOn = Console.ReadLine();
Console.WriteLine();
}
Console.WriteLine();
//------------------------------------------------------------------------------
//檢查custList
count = custList.Count;
for (k = 0; k <= count - 1; k++)
{
Console.WriteLine((k + 1).ToString() + " :" + custList[k] + " ");
}
//------------------------------------------------------------------------------
Console.WriteLine("======================================================");
Console.WriteLine("遞增排序");
// 遞增排序
custList.Sort();
count = custList.Count;
for (k = 0; k <= count - 1; k++)
{
Console.WriteLine((k+1).ToString() + " :" + custList[k] + " ");
}
//------------------------------------------------------------------------------
custList.Reverse();
Console.WriteLine("======================================================");
Console.WriteLine("遞減排序");
count = custList.Count;
for (k = 0; k <= count - 1; k++)
{
Console.WriteLine((k + 1).ToString() + " :" + custList[k] + " ");
}
//------------------------------------------------------------------------------
Console.WriteLine("請輸入欲查詢的客戶名稱: ");
custName = Console.ReadLine();
Console.WriteLine();
// 使用BinarySearch
int index;
index = custList.BinarySearch(custName);
string YN = "N";
if (index >= 0)
{
Console.WriteLine("第{0}客戶的資料是: {1}", index + 1, custList[index]);
Console.WriteLine();
Console.WriteLine("是否刪除該資料?");
YN = Console.ReadLine();
if (YN.ToUpper() == "Y")
{
custList.Remove(custName);
}
}
else
{
Console.WriteLine("無此客戶名稱!");
Console.WriteLine("是否插入該資料?");
YN = Console.ReadLine();
if (YN.ToUpper() == "Y")
{
custList.Insert(count, custName);
}
}
//-----------------------------------------------------------------
count = custList.Count;
for (k = 0; k <= count - 1; k++)
{
Console.WriteLine((k + 1).ToString() + " :" + custList[k] + " ");
}
Console.ReadLine();
}
}
}
測試1:
刪除客戶名單
測試2:新客戶->新增
3. 若資料有重複時, 刪除僅會刪掉其中一筆



留言列表
