You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
/* *
* F i l e : h a s h _ m a p . s w i f t
* C r e a t e d T i m e : 2 0 2 3 - 0 1 - 1 6
* A u t h o r : n u o m i 1 ( n u o m i 1 @ q q . c o m )
*/
import utils
@ main
enum HashMap {
/* D r i v e r C o d e */
static func main ( ) {
/* 初 始 化 雜 湊 表 */
var map : [ Int : String ] = [ : ]
/* 新 增 操 作 */
// 在 雜 湊 表 中 新 增 鍵 值 對 ( k e y , v a l u e )
map [ 12836 ] = " 小哈 "
map [ 15937 ] = " 小囉 "
map [ 16750 ] = " 小算 "
map [ 13276 ] = " 小法 "
map [ 10583 ] = " 小鴨 "
print ( " \n 新增完成後,雜湊表為 \n Key -> Value " )
PrintUtil . printHashMap ( map : map )
/* 查 詢 操 作 */
// 向 雜 湊 表 中 輸 入 鍵 k e y , 得 到 值 v a l u e
let name = map [ 15937 ] !
print ( " \n 輸入學號 15937 ,查詢到姓名 \( name ) " )
/* 刪 除 操 作 */
// 在 雜 湊 表 中 刪 除 鍵 值 對 ( k e y , v a l u e )
map . removeValue ( forKey : 10583 )
print ( " \n 刪除 10583 後,雜湊表為 \n Key -> Value " )
PrintUtil . printHashMap ( map : map )
/* 走 訪 雜 湊 表 */
print ( " \n 走訪鍵值對 Key->Value " )
for ( key , value ) in map {
print ( " \( key ) -> \( value ) " )
}
print ( " \n 單獨走訪鍵 Key " )
for key in map . keys {
print ( key )
}
print ( " \n 單獨走訪值 Value " )
for value in map . values {
print ( value )
}
}
}