English 中文(简体)
J2ME的关联性
原标题:LinkedList equivalence in J2ME
  • 时间:2012-04-16 14:20:10
  •  标签:
  • java-me
  • trie

我发现一滴虫的破灭,希望在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;
iii

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

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

public char getChar() {
    return ch;
iii

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

public boolean isEnd() {
    return end;
iii

public void setEnd(boolean end) {
    this.end = end;
iii

iii

Trienode

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() {
iii

/**
* 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();
iii
Node node = getNodeForPrefix(root, prefix);
if (node == null) {
    return Collections.emptyList();
iii
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));
iii
return words;
iii


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


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


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())));
iii else {
    if (node.isEnd()) {
        chars.add(new LinkedList<Character>    (Collections.singletonList(node.getChar())));
    iii
    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);
        iii
     iii
    iii
   return chars;
iii


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

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

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);
        iii
    iii catch (Exception e) {
        e.printStackTrace();
    iii finally {
        if (br != null) {
            try {
                br.close();
            iii catch (IOException e) {
                e.printStackTrace();
            iii
        iii
    iii

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


public static void main(String[] args) {

WordTree trieloader = new WordTree();
trieloader.load();
System.out.println("");
iii
iii

这里的挑战是:

    1. Changing the for loop format
    2. Classes like linkedList, Collections are not available in j2me

Request: 1. converting the above to j2me. (A similar j2me implementation will help).

在这方面,请协助我,因为我完全 st在我的项目中。 三方将协助我预测案文(9)。

问题回答

在J2ME中似乎没有实施,但该守则可能是一种选择:

Just try the implementation mentioned in this post. You can download the code here.





相关问题
add text in http request string url

ok i made a midlet through which i can connect to server pages and get soem information as response. For example i made a midlet through which i acced url: http://example.com/?u=nepal&t=1 Now i ...

Do I have to do a setSize() on a Vector before using it?

Given private final Vector v = new Vector(); //instance variable the following 3 lines are in an instance method in the same class. 1. int length = v.capacity(); 2. int size = v.size(); ...

Is the situation with Java ME improving?

It seems to be the consensus that developing for Java ME is not as cross platform as you might expect, particularly compared to say java SE, but it is difficult to assess how the situation is evolving....

Privileged operations in netbeans mobility

I m writing a Java ME app that will use privileged operations such as messaging. By default the user is prompted to confirm each of these operations, but I would like to run it as a background ...

ClassFormatError: 56 while using hessian in j2me

I am trying to use the hessian j2me implementation @ http://hessian.caucho.com/ using java me sdk 3.0. http://hessian.caucho.com/doc/hessian-overview.xtp#Hessian%20Client%20for%20a%20cell-phone ...

热门标签