.
雜湊表(Hash table,也叫哈希表),是根據鍵(Key)而直接查詢在內存存儲位置的資料結構。也就是說,它通過計算一個關於鍵值的函數,將所需查詢的數據映射到表中一個位置來查詢記錄,這加快了查找速度。這個映射函數稱做雜湊函數,存放記錄的數組稱做雜湊表。
此篇主要以 ListView 來呈現從Hash table(雜湊表)中尋找內存的資料
預先在雜湊表中存入以下資料,從雜湊表中根據KEY選取資料並訂出資料架構
資料架構如下 一個區域有五個物件,每個物件個別有其數量
根據下圖從雜湊表找出資料並篩選出個別類型物件的數量
實作程式碼如下
- 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;
- using System.Threading;
-
- namespace HashTable_example
- {
- public partial class Form1 : Form
- {
- //設定 console輸出
- [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
- static extern bool AllocConsole();
-
- [System.Runtime.InteropServices.DllImport("Kernel32")]
- public static extern void FreeConsole();
- public Form1()
- {
- InitializeComponent();
- }
-
- Hashtable _all = new Hashtable();
- Hashtable _fish = new Hashtable();
- Hashtable _cat = new Hashtable();
- Hashtable _mountain = new Hashtable();
- Hashtable _tree = new Hashtable();
- Hashtable _lake = new Hashtable();
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- //定義 listview 欄位與型態
- listView1.GridLines = true;
- listView1.FullRowSelect = true;
- listView1.View = View.Details;
- listView1.Scrollable = true;
- listView1.MultiSelect = false; // 不可以多行選擇
- listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
-
- listView1.Columns.Add("Fish", 100, HorizontalAlignment.Right);
- listView1.Columns.Add("Cat", 100, HorizontalAlignment.Left);
- listView1.Columns.Add("Mountain", 120, HorizontalAlignment.Left);
- listView1.Columns.Add("Tree", 100, HorizontalAlignment.Left);
- listView1.Columns.Add("Lake", 100, HorizontalAlignment.Left);
- listView1.Visible = true;
-
- listView2.GridLines = true;
- listView2.FullRowSelect = true;
- listView2.View = View.Details;
- listView2.Scrollable = true;
- listView2.MultiSelect = false; // 不可以多行選擇
- listView2.HeaderStyle = ColumnHeaderStyle.Nonclickable;
- listView2.Visible = true;
-
- String[] item_content = new String[5]; //宣告行數
- ListViewItem item; // 宣告項目
-
- //加入雜湊資料
- _all.Add("Fish", 5);
- _all.Add("Cat", 3);
- _all.Add("Mountain", 3);
- _all.Add("Tree", 5);
- _all.Add("Lake", 2);
-
- _fish.Add("Grass carp", 142);
- _fish.Add("Peruvian anchoveta", 200);
- _fish.Add("Silver carp", 385);
- _fish.Add("Common carp", 499);
- _fish.Add("Alaska pollock", 56);
-
- _cat.Add("Abyssinian", 151);
- _cat.Add("American Bobtail", 33);
- _cat.Add("Balinese-Javanese ", 42);
-
- _mountain.Add("Everest", 1);
- _mountain.Add("Kangchenjunga", 1);
- _mountain.Add("Lhotse", 1);
-
- _tree.Add("Tulsi Plant", 1120);
- _tree.Add("Amla Plant", 932);
- _tree.Add("Eucalyptus", 421);
- _tree.Add("Mahagony", 563);
- _tree.Add("Aloe Vera", 116);
-
- _lake.Add("Finn", 1);
- _lake.Add("Austen", 1);
-
- //AllocConsole(); //啟用 console輸出
-
- int index = 0;
- //將雜湊資料顯示在listview上
- while (index < _all.Count) //每列進行比較 找到當前欄位最大值表示結束
- {
- //如果在雜湊中有找到當前欄位符合標頭,將標頭所屬資料加入 listview中
- foreach (DictionaryEntry member in _all)
- {
- if (member.Key.ToString() == listView1.Columns[index].Text)
- {
- item_content[index] = member.Value.ToString();
- index++;
- break;
- }
- }
- }
-
- //將資料加入項目
- item = new ListViewItem(item_content);
- //將項目加入listview
- listView1.Items.Add(item);
- }
-
- private void listView1_Click(object sender, EventArgs e)
- {
- Point mousePos = listView1.PointToClient(Control.MousePosition);
- ListViewHitTestInfo hitTest = listView1.HitTest(mousePos);
- int columnIndex = hitTest.Item.SubItems.IndexOf(hitTest.SubItem);
- txt_selected_item.Text = listView1.Items[0].SubItems[columnIndex].Text;
- txt_selected_value.Text = listView1.Columns[columnIndex].Text;
-
- Hashtable select = new Hashtable();
- if (txt_selected_value.Text == "Fish") { select = _fish; }
- else if (txt_selected_value.Text == "Cat") { select = _cat; }
- else if (txt_selected_value.Text == "Mountain") { select = _mountain; }
- else if (txt_selected_value.Text == "Tree") { select = _tree; }
- else { select = _lake; }
-
- listView2.Clear();
- foreach (DictionaryEntry member in select)
- {
- listView2.Columns.Add(member.Key.ToString(), 100,
- HorizontalAlignment.Right);
- }
-
- String[] item_content = new String[5]; //宣告行數
- ListViewItem item; // 宣告項目
- int index = 0;
- //將雜湊資料顯示在listview上
- while (index < select.Count) //每列進行比較 找到當前欄位最大值表示結束
- {
- //如果在雜湊中有找到當前欄位符合標頭,將標頭所屬資料加入 listview中
- foreach (DictionaryEntry member in select)
- {
- if (member.Key.ToString() == listView2.Columns[index].Text)
- {
- listView2.Columns[index].TextAlign = HorizontalAlignment.Center;
- item_content[index] = member.Value.ToString();
- index++;
- break;
- }
- }
- }
-
- //將資料加入項目
- item = new ListViewItem(item_content);
- //將項目加入listview
-
- listView2.Items.Add(item);
- AutoSizeColumnList(listView2);
-
- }
-
- //自動對齊 ListView 依據標題長度和listview大小
- private void AutoSizeColumnList(ListView listView)
- {
- //Prevents flickering
- listView.BeginUpdate();
-
- Dictionary<int, int> columnSize = new Dictionary<int, int>();
-
- //Auto size using header
- listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
-
- //Grab column size based on header
- foreach (ColumnHeader colHeader in listView.Columns)
- columnSize.Add(colHeader.Index, colHeader.Width);
-
- //Auto size using data
- listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
-
- //Grab comumn size based on data and set max width
- foreach (ColumnHeader colHeader in listView.Columns)
- {
- int nColWidth;
- if (columnSize.TryGetValue(colHeader.Index, out nColWidth))
- colHeader.Width = Math.Max(nColWidth, colHeader.Width);
- else
- //Default to 50
- colHeader.Width = Math.Max(50, colHeader.Width);
- }
-
- listView.EndUpdate();
- }
-
- }
- }
執行結果
篩選出所屬魚類的種類和數量
篩選出所屬湖泊的名稱和數量![]()
篩選出所屬貓的種類和數量![]()
篩選出所屬山的名稱和數量![]()
篩選出所屬樹的名稱和數量![]()