Eclipse着色方案--Tango
程序员是逻辑人?

J2ME树形结构实现

Thomas posted @ Dec 10, 2009 04:58:54 PM in Programme with tags java j2me 数据结构 , 1735 阅读

        j2me这个东西很小,小到连基本的数据结构也只保留了一个vector,当然是没有泛型的,也就是vector里的东西就是一个Object。所以很多时候我们都要自己实现特性的数据结构,来达到我们的目的,当然如果只是一般的链式结构数组完全可以胜任,这个数组其实和C++比起来那可强的不是一点半点那么简单哦。

        下面我们来实现一个树形结构。

文章来源:J2ME手机游戏开发站

        

这个类实现简单,没有包含复杂的功能,仅仅用来做树形数据的存储还是不错的。比如游戏中用来管理场景,管理资源;应用中用来作分类数据的表现等等。完全足以胜任。

 

/**
 *
 * @author hunhun1981
 */
public class HTree {
 
    private HNode root;
 
    private HNode current;
 
    private int currDepth;
 
    private int maxDepth;
 
    public HTree(Object rootValue) {
        root = new HNode(null, rootValue);
        current = root;
    }
 
    public void goRoot() {
        current = root;
        currDepth = 0;
    }
 
    public boolean goChild(int index) {
        if (current.childList != null) {
            if (current.childList.size() > 0
                && index < current.childList.size()) {
                current = (HNode) current.childList.elementAt(index);
                currDepth++;
                if (currDepth > maxDepth) {
                    maxDepth = currDepth;
                }
                return true;
            }
        }
        return false;
    }
 
    public void goBack() {
        if (current.father != null) {
            current = current.father;
            currDepth –;
        }
    }
 
    public Object getCurrent() {
        return current.value;
    }
 
    public int getCurrentDepth() {
        return currDepth;
    }
 
    public int getMaxDepth() {
        return maxDepth;
    }
 
    public Object[] getChilds() {
        if (current.childList != null) {
            if (current.childList.size() > 0) {
                Object[] ret = new Object[current.childList.size()];
                for (int i = 0; i < ret.length; i++) {
                    ret[i] = ((HNode) current.childList.elementAt(i)).value;
                }
                return ret;
            }
        }
        return null;
    }
 
    public Object getChild(int index) {
        if (current.childList != null) {
            if (current.childList.size() > 0
                && index < current.childList.size()) {
                return ((HNode) current.childList.elementAt(index)).value;
            }
        }
        return null;
    }
 
    public void addChild(Object obj) {
        if (current.childList == null) {
            current.childList = new Vector();
        }
        current.childList.addElement(new HNode(current, obj));
    }
 
    public void addChilds(Object[] objs) {
        if (current.childList == null) {
            current.childList = new Vector();
        }
        for (int i = 0; i < objs.length; i++) {
            current.childList.addElement(new HNode(current, objs[i]));
        }
    }
 
    public int hasChild() {
        if (current.childList == null || current.childList.size() <= 0) {
            return 0;
        } else {
            return current.childList.size();
        }
    }
 
    private class HNode {
 
        public Vector childList;
 
        public HNode father;
 
        public Object value;
 
        public HNode(HNode father, Object value) {
            this.value = value;
            this.father = father;
            this.childList = null;
        }
    }
}

使用方法如下:
HTree tree = new HTree(”root”);//会自动创建一个默认的根节点
tree.addChild(”天才”);//在根节点添加新的节点
tree.addChild(”白痴”);
tree.goChild(0);//进入到当前节点的第一个节点(天才)。
tree.addChild(”天才1号”);//在当前节点(天才)添加新的节点
tree.addChild(”天才2号”);
tree.goBack();//返回当前节点(天才)的父节点(根)
tree.goChild(1);//进入到当前节点的第二个节点(白痴)。
tree.addChild(”白痴1号”);//在当前节点(白痴)添加新的节点
tree.addChild(”白痴2号”);
tree.goRoot();//完成创建后将当前节点设置为根节点。

上面的代码创建了一棵完整的树,当然,您可以使用任何对象代替这里存储的String对象。
还有一些方法,一看函数名大概都能明白,就不再唠叨了。
遍历的方法于上面创建树的方法相似,总之,要注意当前节点的位置,以免下次使用时处在错误的位置。
有兴趣的朋友可以扩展一下遍历方法。不过我觉得没必要。因为J2ME环境下更需要的是树形结构,而不是强大的tree对象。

总之,我比较倾向于简单实现,希望它不太让人觉得简陋就好。从实用出发,它还是能够满足大部分受限平台的需求的。

 

 

本文为原创转载请注明微笑 Creative Commons License
转载时请遵循 “署名-非商业用途-保持一致” 的创作共用协议

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter