Java尝试:需要更好的方法

时间:2016-11-08 08:01:23

标签: java algorithm trie

我是新手,尝试并测试它是如何工作的。现在我正在建立联系人列表。我添加了Prashanth'和' Pradep'对我来说,当我搜索Pra​​' Pra'我应该把这个计算为两个。我的方法是在每个节点中使用变量 size ,并在找到相同长度的字符串时返回它。有一些不必要的东西/变量,比如我用于调试的有效负载等。我发现的问题是当我在hashmap中存储一个字符和一个节点时,会存储空节点。所以我总是得到0作为答案。

 public class tries {

public static class Node {

    HashMap<Character, Node> children = new HashMap<>();
    boolean endOfWord = false;
    int size =0;
    int payload = 10;


    public void setNode(char c, Node n) {
      children.put(c,n);
    }

    public Node getNode(char c) {
        return children.get(c);
    }

    public void addNode( String s,int index) {
        Node current = children.get(s.charAt(index));
        size++;
        if(index ==s.length()-1)
        {
            endOfWord = true;
            return;
        }
        if(current== null)

        {
             current.payload = 11;
            this.setNode(s.charAt(index),current);
        }
        addNode(s,index+1);

    }

    public int findcount(String s, int index) {
        Node current = children.get(s.charAt(index));

        if(index ==s.length()-1)
        {
            current.endOfWord = true;
            return current.size;
        }
        if(current == null)
        {
            return 0;
        }


        return findcount(s,index+1);
    }
}
public static void main(String args[])
{
    String c1 = "Prashanth";
    String c2=  "Pradep";
    Node n = new Node();
    //tries t = new tries();

    n.addNode(c1,0);
    n.addNode(c2,0);

    System.out.println(n.findcount("Pra",0));


}

}

  

我得到的错误如果我运行此代码。 (如果我改变一点,我会得到0)

Exception in thread "main" java.lang.NullPointerException
at tries$Node.addNode(tries.java:35)
at tries.main(tries.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

2 个答案:

答案 0 :(得分:0)

您可以这样做:

import java.util.HashMap;
import java.util.Map;


public class ContactList {

    Map<String, String> contactList; 

    public ContactList() {
        //initialize the map of contact lists. 
        contactList = new HashMap<String, String>();
    }

    public void addContact(String name, String number){
        contactList.put(name, number);
    }

    public Map<String,String> findbyName(String name){

        Map<String,String> resultMap = new HashMap<String, String>();

        for(String key : contactList.keySet()){
            //trim and make the name lowercase, then compare it to the lowercased String
            if(key.trim().toLowerCase().contains(name.toLowerCase())){
                resultMap.put(key, contactList.get(key));
            }
        }
        return resultMap;

    }

    public int countResult(String name){
        //return size of found map
        return findbyName(name).size();
    }



}

然后在main方法上使用它:

public class ContactMain {

    public static void main(String args[])
    {
        String c1 = "Prashanth";
        String c2=  "Pradep";
        ContactList contacts = new ContactList();
        contacts.addContact(c1, "1234567890");
        contacts.addContact(c2, "12345678");
        System.out.println(contacts.countResult("Pra"));

    }
}

答案 1 :(得分:0)

您检查current是否为空,如果是,则调用其中的内容。见这个块:

   if(current== null)
    {
         current.payload = 11;
        this.setNode(s.charAt(index),current);
    }

注意current == null然后current.payload。你不能在null上调用任何东西,它代表什么都没有:)

您可能要做的是创建一个新的Node,对其进行初始化,将其放入地图,然后将其分配给current