Java me J2ME中的LinkedList等价性

Java me J2ME中的LinkedList等价性,java-me,trie,Java Me,Trie,我发现了一个trie的java实现,并希望在J2ME中有一个类似的实现。这是代码 节点类 import java.util.Collections; import java.util.LinkedList; import java.util.List; class Node { private final char ch; /** * Flag indicates that this node is the end of the string. */ private boolean e

我发现了一个trie的java实现,并希望在J2ME中有一个类似的实现。这是代码

节点类

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class Node {

private final char ch;

/**
 * Flag indicates that this node is the end of the string.
 */
private boolean end;

private LinkedList<Node> children;

public Node(char ch) {
    this.ch = ch;
}

public void addChild(Node node) {
    if (children == null) {
        children = new LinkedList<Node>();
    }
    children.add(node);
}

public Node getNode(char ch) {
    if (children == null) {
        return null;
    }
    for (Node child : children) {
        if (child.getChar() == ch) {
            return child;
        }
    }
    return null;
}

public char getChar() {
    return ch;
}

public List<Node> getChildren() {
    if (this.children == null) {
        return Collections.emptyList();
    }
    return children;
}

public boolean isEnd() {
    return end;
}

public void setEnd(boolean end) {
    this.end = end;
}
请求: 1.将上述内容转换为j2me。(类似的j2me实现将有所帮助)


请帮助我,因为我完全陷入了我的项目。trie将帮助我进行文本预测(t9)

在J2ME中似乎没有内置实现,但这段代码可能是一个选项:

只需尝试中提到的实现。
你可以下载代码。

相关:谢谢你的链接,我现在可以导入linkedList了。将for循环转换为可以在j2me中使用的形式现在是我的主要挫折。非常感谢您的进一步帮助,如果我错了,请纠正我。。。但是,在J2ME中,不可能像您那样(从JavaSE中知道)使用(每个)。但是你可以用“标准”来代替循环。(链接中的示例使用了一个while循环,它的使用方式与for循环非常类似。请查看此处以获取有关j2me限制的更多信息:……如果您认为我的帖子有帮助,请不要忘记向上投票。;-(单击我答案附近的向上箭头)您的帖子很有帮助,但还没有此类特权。
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class WordTree {
Node root = new Node(' ');

public WordTree() {
}

/**
* Searches for a strings that match the prefix.
*
* @param prefix - prefix
* @return - list of strings that match the prefix, or empty list of no matches are        found.
*/
public List<String> getWordsForPrefix(String prefix) {
if (prefix.length() == 0) {
    return Collections.emptyList();
}
Node node = getNodeForPrefix(root, prefix);
if (node == null) {
    return Collections.emptyList();
}
List<LinkedList<Character>> chars = collectChars(node);
List<String> words = new ArrayList<String>(chars.size());
for (LinkedList<Character> charList : chars) {
    words.add(combine(prefix.substring(0, prefix.length() - 1), charList));
}
return words;
}


private String combine(String prefix, List<Character> charList) {
StringBuilder sb = new StringBuilder(prefix);
for (Character character : charList) {
    sb.append(character);
}
return sb.toString();
}


private Node getNodeForPrefix(Node node, String prefix) {
if (prefix.length() == 0) {
    return node;
}
Node next = node.getNode(prefix.charAt(0));
if (next == null) {
    return null;
}
return getNodeForPrefix(next, prefix.substring(1, prefix.length()));
}


private List<LinkedList<Character>> collectChars(Node node) {
List<LinkedList<Character>> chars = new ArrayList<LinkedList<Character>>();

if (node.getChildren().size() == 0) {
    chars.add(new LinkedList<Character>(Collections.singletonList(node.getChar())));
} else {
    if (node.isEnd()) {
        chars.add(new LinkedList<Character>    (Collections.singletonList(node.getChar())));
    }
    List<Node> children = node.getChildren();
    for (Node child : children) {
        List<LinkedList<Character>> childList = collectChars(child);

        for (LinkedList<Character> characters : childList) {
            characters.push(node.getChar());
            chars.add(characters);
        }
     }
    }
   return chars;
}


public void addWord(String word) {
addWord(root, word);
}

private void addWord(Node parent, String word) {
if (word.trim().length() == 0) {
    return;
}
Node child = parent.getNode(word.charAt(0));
if (child == null) {
    child = new Node(word.charAt(0));
    parent.addChild(child);
}
if (word.length() == 1) {
    child.setEnd(true);
} else {
    addWord(child, word.substring(1, word.length()));
}
}

public void load() {
    WordTree tree = new WordTree();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(new    File("C:/Users/Sironka/Documents/NetBeansProjects/Final Maa Adaptive System/dictionary/Maa    Corpus.txt-02-ngrams-Freq.txt")));
        String eachLine = null;
        while ((eachLine = br.readLine()) != null) {
            tree.addWord(eachLine);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println(tree.getWordsForPrefix("ore"));
  }


public static void main(String[] args) {

WordTree trieloader = new WordTree();
trieloader.load();
System.out.println("");
}
}
    1. Changing the for loop format
    2. Classes like linkedList, Collections are not available in j2me