跳到主要內容

C#教材(10) 陣列與集合

陣列與集合


之前有提過變數問題,但是當我們有一群相同資料型別,例如: 班上數學成績、
客戶名單等等。我們使用變數一個個儲存太過麻煩且沒有效率,這一類的資料
我們可以用陣列或是集合來做存取。
  • 陣列
  陣列是用來儲存資料群,我們可以將一個資料稱為元素,每個元素都有他們的
  位置方便我們存取,我們稱為索引值。   

(1) 一維陣列

陣列宣告方式:
                         DataType[] ArrayName = new DataType[] 

                         Ex: int[] men = new int[length] ;

初始化 :
     (i)預先給值
                         Ex:  int[] pins = new int[4] {9,3,7,2};

     (ii)個別元素
                         Ex:  int[] pins = new int[4] ;
                                 
 
讀取單一元素:
                        Ex:  pins[0]   //讀取第一個元素 ,結果為 : 9
                               pins[1]   //讀取第二個元素,結果為 : 3  
                               pins[2]   //讀取第三個元素 ,結果為 : 7
讀取全部元素:

     (i) FOR
int[] intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 for (int i = 0; i < intArray.Length; i++)
 {
     Console.Write(intArray[i] + ",");
  }

    (ii) FOREACH
    用來讀取陣列所有的元素,他會逐一檢視陣列的變數值,將他逐一將元素

int[] intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

     foreach(int item in intArray)
{
Console.Write(item+",") ; 
}

(2) 多維陣列
                              DataType[,] ArrayName = new DataType[int d1, int d2] 
                              DataType[, ,] ArrayName = new DataType[int d1, int d2, int d3             
     (i)預先給值
                         Ex:   int[,] intMArray = new int[2, 4]{{1,2,3,4},{5,6,7,8}};

     (ii)個別元素
int[,] intMArray2 = new int[2, 4];
intMArray2[0, 2] = 10;
intMArray2[0, 3] = 20;
intMArray2[1, 3] = 100;

(3) 無固定長度陣列
                              DataType[][] ArrayName = new DataType[int d1][] 
                              DataType[] ArrayName = new DataType[int d2] 
                              DataType[] ArrayName = new DataType[int d3] 

    Ex:
  string[][] strArray = new string[4][];
strArray[0] = new string[3] { "apple", "banana", "pineapple" };
strArray[1] = new string[4] { "novel", "cartoon", "managize", "education" };

foreach (string[] onelayer in strArray)
{
                                //因為只有規劃兩列的陣列,所以當為null就不做取出
if (onelayer == null)
{
break;
}
foreach (string item in onelayer)
{
Console.Write(item+",");
}
Console.WriteLine();
}

  • System.Array類別
當我們建立陣列時,有些屬性或是方法他會自動的繼承,例如:GetValue取的元素值、
Copy複製物件等等。
    參考:http://msdn.microsoft.com/zh-tw/library/system.array.aspx
   
   宣告方式:
    int[] intArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int[] copyArray = new int[10] ;

方法 說明 例子
void Copy(SourceArray, DestinactionArray, Int64)
void Copy(SourceArray, DestinactionArray, Int32)
複製陣列  Array.Copy(intArray, copyArray, 5);
//copyArray 內容為 1,2,3,4,5

void  Copy(Array, Int32, Array, Int32, Int32)
void  Copy(Array, Int32, Array, Int64, Int64)
說明 例子
int GetLength(Int64) 取得陣列的長度 intArray.GetLength(0)  //10
Object GetValue(Int64) 取得元素值 intArray.GetValue(1) // 2
Reverse(Array) 逆排序 Reverse// 10,9,8,7,6,5,4,3,2,1
Sort(Array) 順排序 Reverse // 1,2,3,4,5,6,7,8,9,10


建立隱含型別 :
   var names = new[] {"John" , "Diana" , "James"} ;
   var numbers = new[] {1 , 2 , 3.5 , 99.99} ;


  • 集合
集合可以說是物件的集合,他可以一次處理多個物件,.NET將物件規劃在System.Collection
你可以透過實作介面或是利用現成的方法去完成,集合的處理。接下來我們介紹集合幾個
重要的類別及介面:



  1. ArrayList

ArrayList是實作List介面的的類別,他跟Array很相似,陣列宣告時需要給定明確長度,但是
如果我們對一組資料可能沒有辦法決定明確的長度,這是建議可以使用ArrayList來宣告。

     //規劃員工類別: 職工編號、年紀、薪水
      class Employee :IComparable
{
public String no;
public int age;
public int salary;
}

        ArrayList alist = new ArrayList();
Console.WriteLine("初始化的長度: {0}",alist.Count) ;

//輸入員工清單
Console.WriteLine("===輸入員工清單===");
for (int i = 0; i <= 7; i++)
{
Random random = new Random() ;
Employee employee = new Employee() ;
employee.no = String.Format("{0}", i);
System.Threading.Thread.Sleep(500);
employee.age = random.Next(18, 65);
employee.salary = random.Next(15000, 50000);
alist.Add(employee);
}

//插入空降部隊
alist.Insert(3, new Employee { no="8" , salary=50000, age=30 });

//列出員工清單
foreach (Employee emp in alist)
{
Console.Write("編號:{0},",emp.no) ;
Console.Write("年齡:{0},", emp.age);
Console.Write("薪水:{0},", emp.salary);
Console.WriteLine() ;
}

Console.WriteLine("===移除空降部隊===");
alist.Remove(alist[3]);  //移除空降部隊

Console.WriteLine("===組織改選===");
alist.Reverse();   //組織改選

//列出員工清單
foreach (Employee emp in alist)
{
Console.Write("編號:{0},", emp.no);
Console.Write("年齡:{0},", emp.age);
Console.Write("薪水:{0},", emp.salary);
Console.WriteLine();
}

結果:



2. IComparable介面
   這個介面主要可以用來實現排序的功能,依據某個值是否大於、 等於或小於另傳
   回 1、 0 或-1

    //實作年齡 IComparable介面
  class Employee :IComparable
{
public String no;
public int age;
public int salary;

public int CompareTo(object obj)
{
Employee refEmp = (Employee)obj;
return this.age.CompareTo(refEmp.age);
}
}

ArrayList alist = new ArrayList();
Console.WriteLine("初始化的長度: {0}",alist.Count) ;

//輸入員工清單
Console.WriteLine("===輸入員工清單===");
for (int i = 0; i <= 7; i++)
{
Random random = new Random() ;
Employee employee = new Employee() ;
System.Threading.Thread.Sleep(500);
employee.no = random.Next(1, 20).ToString();
employee.age = random.Next(18, 65);
employee.salary = random.Next(15000, 50000);
alist.Add(employee);
}

//插入空降部隊
alist.Insert(3, new Employee { no="8" , salary=50000, age=30 });

//員工以年齡排序
alist.Sort();
//列出員工清單
foreach (Employee emp in alist)
{
Console.Write("編號:{0},",emp.no) ;
Console.Write("年齡:{0},", emp.age);
Console.Write("薪水:{0},", emp.salary);
Console.WriteLine() ;
}

3. 列舉
逐一列舉出元素,集合常用的模式,而IEnumberator負責這些方法的定義,如MoveNext、
Reset、MoveNext等等。

        class EnumEx1:IEnumerable
{
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}

實例:
      public class Employee : IComparable, IEnumerable, IEnumerator
{
public String no;
public int age;
public int salary;

int currentIndex = -1;
ArrayList empList = new ArrayList();

public Employee()
{

}

public Employee(ArrayList empolyeeArrayList)
{
this.empList = empolyeeArrayList;
}

public int CompareTo(object obj)
{
Employee refEmp = (Employee)obj;
return this.age.CompareTo(refEmp.age);
}

//IEnumerable
public IEnumerator GetEnumerator()
{
return this.empList.GetEnumerator();
}

//IEnumerator
public object Current
{
get { return empList[currentIndex]; }
}

public bool MoveNext()
{
bool result = false;
if (empList.Count >= currentIndex)
{
currentIndex += -1;
result = true;
}

return result;
}

public void Reset()
{
currentIndex = -1;
}
}

  class ArrayListEx
{
public static void Main()
{
ArrayList alist = new ArrayList();
Console.WriteLine("初始化的長度: {0}",alist.Count) ;

//輸入員工清單
Console.WriteLine("===輸入員工清單===");
for (int i = 0; i <= 7; i++)
{
Random random = new Random() ;
Employee employee = new Employee() ;
System.Threading.Thread.Sleep(500);
employee.no = random.Next(1, 20).ToString();
employee.age = random.Next(18, 65);
employee.salary = random.Next(15000, 50000);
alist.Add(employee);
}

Employee empAction = new Employee(alist);
IEnumerator enumtor = empAction.GetEnumerator();

while(enumtor.MoveNext())
{
Employee employee = (Employee)enumtor.Current;
Console.WriteLine("no:"+employee.no + "\t");
Console.WriteLine("age:"+employee.age + "\t");
Console.WriteLine("salary:"+employee.salary + "\t");
}
}




常用的介面

  1. ICollection介面
當我們實作ICollection介面時,我們需要實作下列的方法。


class CollectionEx1 : ICollection
{
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}

public int Count
{
get { throw new NotImplementedException(); }
}

public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}

public object SyncRoot
{
get { throw new NotImplementedException(); }
}

public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}

      2.IList

      class IListExt :IList
{
public int Add(object value)
{
throw new NotImplementedException();
}

public void Clear()
{
throw new NotImplementedException();
}

public bool Contains(object value)
{
throw new NotImplementedException();
}

public int IndexOf(object value)
{
throw new NotImplementedException();
}

public void Insert(int index, object value)
{
throw new NotImplementedException();
}

public bool IsFixedSize
{
get { throw new NotImplementedException(); }
}

public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}

public void Remove(object value)
{
throw new NotImplementedException();
}

public void RemoveAt(int index)
{
throw new NotImplementedException();
}

public object this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}

public int Count
{
get { throw new NotImplementedException(); }
}

public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}

public object SyncRoot
{
get { throw new NotImplementedException(); }
}

public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}






  • 堆疊與促列(stack & queue)

(1) 堆疊(stack)
    堆疊是一種後進先出的資料結構,就是跟發牌的排列一樣,我們可以利用stack的類別
    去使用,他分別實作ICollection、IEnumerable和ICloneable這三種介面。stack提供兩個重
    要的方法Pop和Push,Pop為放入以及Push為取出。
   
    Ex:
           Stack numberStack = new Stack();
            numberStack.Push("dog");
            numberStack.Push("a");
            numberStack.Push("is");
            numberStack.Push("it");
            Console.WriteLine("Count:{0} ", numberStack.Count);

            Console.WriteLine("{0} ", numberStack.Pop());
            Console.WriteLine("{0} ", numberStack.Pop());
            Console.WriteLine("{0} ", numberStack.Pop());
            Console.WriteLine("{0} ", numberStack.Pop());
   
     Ans : it is a dog


 (2)促列(queue)
 促列是一種先進先出的資料結構,最先加入會先被取出,例如: 排隊、網路資料處理等等
 Queue分別實作ICollection、IEnumerable和ICloneable這三種介面。queue的預設容量大小32。
 queue提供兩個重要的方法enqueue和dequeue,enqueue為放入以及dequeue為取出。

        public static void Main()
        {
            Queue queue = new Queue();
            queue.Enqueue("it");
            queue.Enqueue("is");
            queue.Enqueue("a");
            queue.Enqueue("dog");

            Console.WriteLine("{0}",queue.Dequeue());
            Console.WriteLine("{0}", queue.Dequeue());
            Console.WriteLine("{0}", queue.Dequeue());
            Console.WriteLine("{0}", queue.Dequeue());
        }

 Ans : it is a dog



(3)雜湊(hashtable)
 它是一種資料結構的方式,透過數學方函式運算,將集合中的元素經由雜湊函式轉換
 來對應表格中的索引值。















留言

這個網誌中的熱門文章

JavaBean 和POJO

前言 今天介紹JavaBean和POJO的不同,這兩個名詞在JAVA文章常常被拿來使用以及討論。在JDK1.1時候釋出才有的一個標準架構,很多時候常常被搞混,所以我們特別開闢一章來加以討論。POJO規範在企業級應用已經廣大的被使用的規範。 解釋 POJO : 全名為Plain-old-Java-object,只需要繼承Object就可以,沒有特定規定,只要建立的類別有setter/getter方法都可以稱為POJO JavaBean: JavaBean通常用來封裝多個物件成為單獨物件使用,規範比較嚴格,規則如下 規則 說明 1 需要實作序列(Serializable/Externalizable) 2 不能有參數的建構子( no-arg constructor) 3 需要有公用setter/getter 4 屬性必須要私人(private) 5 屬於特定POJO規則 比較 所有的JavaBean都為POJO,但是所有的POJO不一定為JavaBean 都可以當作重複元件 都必須序列化 特性都為可用性、易用性和持久化使用 - 應用 由圖我們可以知道POJO在應用程式中,主要用來存取資料庫資料達到持久化的目的,並提供給商業邏輯流程處理使用。這種POJO的架構提供程式人員開發時的可以很有規則將資料封裝並加以使用。 範例1. JavaBean(以員工為實例) JavaBean建立員工物件,可以發現Employee物件建構子沒有任何參數,屬性為私有化並setter/getter的命名方式。 //實作序列化 public class Employee implements java.io.Serializable{ private int id; private String name; //無參數建構子 public Employee(){} //以下實作setter/getter public void setId(int id){this.id=id;} public int getId(){return id;} public void setName(String ...

GSON基礎教學

GSON 前言 JSON是很常見的資料交換格式,在JAVA領域常用處理JSON的函式庫:GSON、FastXML和JSON-B,本章節會以GSON為主,學習目標如下 JSON格式說明 GSON 套件函式 GSON: 物件轉換JSON字串 GSON: JSON字串轉換物件 JSON 格式說明 JSON全名為JavaScript Object Notation,它是一種輕量級的資料交換格式,會大為流行的理由,主要是他比傳統用xml更輕巧且容易處理, JSON表達方式物件會用大括弧{},陣列則是用中括號[]。 用JSON字串來表達Employee的物件內容,由JSON字串可以知道物件name、age、sex和salary屬性。 JSON表示員工資料方式: {“name”:”Jack Bryant”, “age”:18, “sex”:”M”,”salary”:3500.00} JSON陣列表示方式: 跟我們使用JAVA的陣列方式類似,內容值可以是數字’、文字、布林、陣列、物件、null等等。 範例: 字串: [“紅”、”橙”、”黃”、”綠”、”青”、”藍”、”紫”} 布林: [true, true, false, false, true, true] GSON 套件函式 Gson為google所發布的函式庫,主要將物件與json字串之間的轉換時方便使用。當我們將JAVA物件轉換成JSON字串稱為 序列化 ,JSON字串轉換至JAVA物件稱為 反序列化 。 GSON: 物件轉換JSON字串 有了JSON基本概念後,我們進入本章重點,首先我們需要建立員工類別(Employee),定義如下 物件 屬性 員工類別 Employee name 名字 age 年紀 sex 性別 salary 薪水 /** * name:員工類別 */ public class Employee implements Serializable { //constructor public Employee(String name, double salary){ this.name = name; this.sala...

H2資料庫(1)-基本安裝與介紹

H2資料庫介紹 H2為嵌入式資料庫,使用java開發,跨平台且內含資料庫管理介面,好處開發階段方便開發人員使用。 比較 詳細比較表: http://www.h2database.com/html/features.html#comparison 由圖比較可以知道,H2比其他資料庫更為優勢,以下會介紹開發時常用的模式: 嵌入式模式Embedded Mode 嵌入式資料庫會與應用程式共用同一JVM底層,在這個模式下persistent或是in-memory資料庫都支援,也沒有連線數的限制。但壞處是只可以給該應用程式使用,其他人無法直接存取資料庫。 伺服器模式 Server Mode 外部應用程式可以藉由JDBC或是OBC的方式連結該資料庫,它也支援persistent或是in-memory資料庫,也沒有連線數的限制。 混合模式 Mixed Mode 同時有嵌入式與伺服器的模式去讓外部應用程式連線或是自己應用程式連線。 安裝H2 database 官方網站: http://www.h2database.com/html/main.html 下載安裝程式 出現安裝連結,本次範例使用 Windows installer 的安裝連結 下載安裝程式後,點擊兩下,進入安裝畫面,按”下一步” 一直按”下一步”後,會出現”完成” 安裝完成後,會出現一個說明網頁,你可以點選Quickstart 它會告訴你,如何進入DBRMS畫面以及開啟資料庫服務 開啟windows的視窗圖示,執行 H2 Console後,會執行H2資料庫 進入DBRMS的登入畫面,使用者預設為”SA”,密碼為空值”“,按下”connect”進入,SQL命令中心 登入後,可以在空白處執行SQL語法 8.大致上安裝H2資料庫滿快速,操作畫面也是很好上手,如果系統在開發階段個人覺的滿推薦給大家使用看看。 參考 H2官方網站