The fundamentals of Hash tables?

前端 未结 11 797
灰色年华
灰色年华 2020-12-07 08:23

I\'m quite confused about the basic concepts of a Hash table. If I were to code a hash how would I even begin? What is the difference between a Hash table and just a normal

11条回答
  •  温柔的废话
    2020-12-07 09:10

    Hashtables are associative. This is a huge difference from arrays, which are just linear data structures. With an array, you might do something like this:

    int[] arr = ...
    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i] + 1);
    }
    

    Notice how you are getting an element out of the array by specifying an exact memory offset (i). This contrasts with hashtables, which allow you to store key/value pairs, later retrieving the value based on the key:

    Hashtable table = new Hashtable();
    table.put("Daniel", 20);
    table.put("Chris", 18);
    table.put("Joseph", 16);
    

    With the above table, we can make the following call:

    int n = table.get("Chris");
    

    ...and be assured that n will be valued at 18.

    I think this will probably answer most of your questions. The implementation of a hashtable is a fairly interesting topic, one which Wikipedia addresses passably well.

提交回复
热议问题