Click here to Skip to main content
14,300,474 members
   

Java

 
GeneralRe: Global variables are not supported in C# Pin
Dr.Walt Fair, PE5-May-19 7:54
professionalDr.Walt Fair, PE5-May-19 7:54 
AnswerRe: Global variables are not supported in C# Pin
Maciej Los27-Mar-19 23:00
protectorMaciej Los27-Mar-19 23:00 
GeneralRe: Global variables are not supported in C# Pin
Brian_TheLion27-Mar-19 23:35
memberBrian_TheLion27-Mar-19 23:35 
AnswerRe: Global variables are not supported in C# Pin
Dr.Walt Fair, PE5-May-19 7:50
professionalDr.Walt Fair, PE5-May-19 7:50 
QuestionHome automation using iot Pin
Member 1417643119-Mar-19 1:49
memberMember 1417643119-Mar-19 1:49 
AnswerRe: Home automation using iot Pin
OriginalGriff19-Mar-19 1:51
protectorOriginalGriff19-Mar-19 1:51 
AnswerRe: Home automation using iot Pin
jschell23-Mar-19 5:48
memberjschell23-Mar-19 5:48 
QuestionStore all words in a Trie into a list Pin
Member 1418712518-Mar-19 13:55
memberMember 1418712518-Mar-19 13:55 
I'm trying to implement a method that will store all words in a Trie data structure in a List.
This is my Node class:
class Node {
    char c;
    HashMap<Character, Node> children = new HashMap<Character, Node>();
    boolean isCompleteWord;

    public Node(char c) {
        this.c = c;
        isCompleteWord = false;
    }
    public Node() {
    }
}
I am using a HashMap to store the child characters as keys and the child nodes as values. A node that completes a word will have the field isCompleteWord set to true.
Methods to add all words to a List:
List<String> collectWords(char c) {
    List<String> words = new ArrayList<>();
    String word = Character.toString(c);
    Node node = root.children.get(c);
    collectWordsHelper(words, node, word);
    return words;
}

void collectWordsHelper(List<String> words, Node node, String word) {
    for (char i = 'a'; i <= 'z'; i++) {
        if (node.children.containsKey(i)) {
            System.out.println(i); // prints the right letters
            Node child = node.children.get(i);
            word += i;
            if (child.isCompleteWord && !words.contains(word)) {
                words.add(word);
                collectWordsHelper(words, child, "");
            }
            collectWordsHelper(words, child, word);
        }
    }
}
Currently if I have stored in the Trie the words "beard", "bold", "brew", when I print the list of words starting with the prefix "b", I get:
[beard, beold, beorew]
What I was expecting:
[beard, bold, brew]
I think I need a way for the String word to be reset whenever I have found a word, instead of the next characters being appended.
GeneralRe: Store all words in a Trie into a list Pin
Richard MacCutchan18-Mar-19 22:55
protectorRichard MacCutchan18-Mar-19 22:55 
QuestionGraph theory algortithm Pin
Member 141708224-Mar-19 8:33
memberMember 141708224-Mar-19 8:33 
QuestionJava 8 or 10 for Windows 8.1 Pin
Brian_TheLion2-Mar-19 11:45
memberBrian_TheLion2-Mar-19 11:45 
AnswerRe: Java 8 or 10 for Windows 8.1 Pin
Richard MacCutchan3-Mar-19 2:36
protectorRichard MacCutchan3-Mar-19 2:36 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Brian_TheLion3-Mar-19 10:53
memberBrian_TheLion3-Mar-19 10:53 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Richard MacCutchan3-Mar-19 21:42
protectorRichard MacCutchan3-Mar-19 21:42 
GeneralRe: Java 8 or 10 for Windows 8.1 Pin
Brian_TheLion4-Mar-19 0:03
memberBrian_TheLion4-Mar-19 0:03 
AnswerMessage Closed Pin
8-Apr-19 18:52
memberdurga65178-Apr-19 18:52 
QuestionWhat attracts people to choose Java as a programming language Pin
Brian_TheLion22-Feb-19 19:13
memberBrian_TheLion22-Feb-19 19:13 
AnswerRe: What attracts people to choose Java as a programming language Pin
OriginalGriff22-Feb-19 19:15
protectorOriginalGriff22-Feb-19 19:15 
GeneralRe: What attracts people to choose Java as a programming language Pin
Brian_TheLion23-Feb-19 13:17
memberBrian_TheLion23-Feb-19 13:17 
GeneralRe: What attracts people to choose Java as a programming language Pin
OriginalGriff23-Feb-19 20:46
protectorOriginalGriff23-Feb-19 20:46 
AnswerRe: What attracts people to choose Java as a programming language Pin
Richard MacCutchan22-Feb-19 21:56
protectorRichard MacCutchan22-Feb-19 21:56 
GeneralRe: What attracts people to choose Java as a programming language Pin
Brian_TheLion23-Feb-19 13:15
memberBrian_TheLion23-Feb-19 13:15 
GeneralRe: What attracts people to choose Java as a programming language Pin
Richard MacCutchan26-Feb-19 3:59
protectorRichard MacCutchan26-Feb-19 3:59 
GeneralRe: What attracts people to choose Java as a programming language Pin
Brian_TheLion26-Feb-19 19:33
memberBrian_TheLion26-Feb-19 19:33 
GeneralRe: What attracts people to choose Java as a programming language Pin
Richard MacCutchan26-Feb-19 21:15
protectorRichard MacCutchan26-Feb-19 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.