阅读背景:

如何在字符串检索 - 哈希表上修复空指针异常

来源:互联网 
class Item
{
    private int address;
    private String itemString;

    public Item(String item)
    {
        separate(item);
    }

    public void separate(String string)
    {
        StringTokenizer st = new StringTokenizer(string);
        itemString = st.nextToken();
        if(st.hasMoreTokens())
        {
            address = Integer.parseInt(st.nextToken());
        }
        else
        {
            address = -1;
        }
    }

    public String getKey()
    {
        return itemString;
    }

    public int getAddress()
    {
        return address;
    }

    public void illegitimize()
    {
        itemString = "*del";
        address = -1;
    }
}

class HashTable
{
    private Item[] hashArray;
    private int arraySize;

    public HashTable(int size)
    {
       arraySize = size;
       hashArray = new Item[arraySize];
    }

    public int hash(Item item)
    {
        String key = item.getKey();

        int hashVal = 0;
        for(int i=0; i<key.length(); i++)
        {
            int letter = key.charAt(i) - 96;
            hashVal = (hashVal * 26 + letter) % arraySize;
        }

        return hashVal;
    }

    public void insert(Item item)
    {
        int hashVal = hash(item);

        while(hashArray[hashVal] != null && 
                !(hashArray[hashVal].getKey().contains("*")))
        {
            hashVal++;
            hashVal %= arraySize;
        }

          String keyAtHashVal = hashArray[hashVal].getKey();
          String itemKey = item.getKey();

        if(!keyAtHashVal.equals(itemKey))
        {
            hashArray[hashVal] = item;
            System.out.println(item.getKey() + " inserted into the table at "
                    + "position " + hashVal);
        }
        else
        {
            System.out.println("Error: " + item.getKey() + " already exists "
                    + "at location " + hashVal);
        }
    }

    public Item find(Item item)
    {
        int hashVal = hash(item);

        while(hashArray[hashVal] != null)
        {
            if(hashArray[hashVal].getKey().equals(item.getKey()))
            {
                System.out.println(item.getKey() + " found at location "
                        + hashVal + " with address " + item.getAddress());
                return hashArray[hashVal];
            }

            hashVal++;
            hashVal %= arraySize;
        }

        System.out.println("Error: " + item.getKey() + " not found in the "
                + "table");
        return null;
    }

}

public class HashTableMain
{
    public static void main(String[] args) throws Exception
    {
        File file = new File(args[0]);
        Scanner input = new Scanner(file);

        Item currentItem;
        String currentItemsKey;
        int currentItemsAddress;

        HashTable table = new HashTable(50);

        while(input.hasNextLine())
        {
            currentItem = new Item(input.nextLine());
            currentItemsKey = currentItem.getKey();
            currentItemsAddress = currentItem.getAddress();

            if(currentItemsAddress > 0)
            {
                table.insert(currentItem);
            }
            else
            {
                table.find(currentItem);
            }
        }
    }
}
class Item
{
    private int address;
    priva



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: