uopspop

Untitled

Nov 15th, 2020 (edited)
1,111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.Hash;
  2.  
  3.  
  4. /**
  5.  *
  6.  * good: space OK!
  7.  * bad: collision ...
  8.  *
  9.  */
  10. public class Hash_001_HashTable {
  11.  
  12.     static int space_limit = 10;
  13.     private Integer[] nums;
  14.     private int[] hash_table;
  15.  
  16.     public Hash_001_HashTable(Integer[] nums) {
  17.         this.nums = nums;
  18.     }
  19.  
  20.     public void bulid_hash_table() {
  21.         this.hash_table = new int[space_limit];
  22.         for (int i = 0; i < nums.length; i++) {
  23.             add(nums[i]);
  24.         }
  25.     }
  26.  
  27.     private void add(Integer num) {
  28.         int index = hash(num);
  29.         int value = num;
  30.         this.hash_table[index] = value;
  31.     }
  32.  
  33.     private int hash(int num){
  34.         return num % 10;
  35.     }
  36.  
  37.     /** find by index **/
  38.     private void hash_search(int target_num) {
  39.         int index = hash(target_num);
  40.         if (this.hash_table[index] == target_num) {
  41.             System.out.println("found(hash): " + target_num);
  42.         }
  43.     }
  44.  
  45.     private static Integer[] get_raw_ary() {
  46.         Integer[] nums = {78,6,80,73,27,61,35,44,29,2};
  47.         return nums;
  48.  
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         // hash search
  53.         int target_num = 29;
  54.         Integer[] nums = get_raw_ary();
  55.         // build hash table
  56.         Hash_001_HashTable hash_table = new Hash_001_HashTable(nums);
  57.         hash_table.bulid_hash_table();
  58.         // search it!
  59.         hash_table.hash_search(target_num);
  60.  
  61.         System.out.println();
  62.     }
  63.  
  64. }
  65.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×