J2me Game开发技巧:树形结构实现

精贴 置顶
1594 0

  树形结构(tree)是比较常用的数据结构了,MIDP中没有它的身影,不然我就不用写这篇文章了。

  代码如下:


   /**
  *
  * @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对象。

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

  • 没有任何评论
今日天气 ···

···

···

···

热门排行
CSS cursor鼠标样式一览表 2012-06-25
.NET后台写JS代码 2011-11-03
网站地址多出jdfwkey的问题解析及... 2010-08-25
数据库xxx的日志已满,请备份该数... 2013-07-19
百度、google、Yahoo网站地图制作... 2011-01-30
<a>标签的伪类书写顺序问题... 2010-09-04
什么是长尾关键词? 2010-09-24
CEO名言 2010-08-31
Mysql 主从数据库同步 2010-09-12
用ASP实现网页BBS 2010-11-01
博主推荐
本个人博客微信公众平台上线啦~~... 2013-10-13
饼哥网络互联上线啦~~要买域名空... 2013-09-20
网站title标题如何正确修改不会被... 2013-08-26
饼哥通讯录系统上线啦,欢迎大家... 2013-08-24
ASP .NET MYSQL 的简单分页 并不... 2013-08-19
网站有弹窗广告这样的站点,百度... 2013-08-17
做淘宝SEO优化需要注意的8大问题 2013-08-17
淘宝网怎么做SEO优化 2013-08-17
站长们要学习的“苍井空精神” 2013-08-17
苹果公司今日发布了iOS 7第五个开... 2013-08-07
随便看看
.net 获取远程IP 得到省市 2013-07-18
IIS 上传文件大小配置步骤(默认2... 2012-05-31
ASP中几种分页显示的比较 2010-11-01
ie6下line-height属性失效与解决... 2010-09-02
.net将IP转换为10进制 2013-07-18
做淘宝SEO优化需要注意的8大问题 2013-08-17
JS复制文本到剪贴板代码 2010-09-13
Android_NetworkInfo以及判断手机... 2013-06-19
JS过滤url参数特殊字符的方法 2013-11-15
学好Java关键的几种方法 2013-06-26
RSS新闻
传媒新闻
CSDN
八卦新闻
女性新闻
台湾新闻
互联网
军事-新浪博客
IT-新浪博客
汽车新闻
游戏新闻
国际新闻
国内新闻
体育新闻
我的微博
北京 上海 杭州 深圳 广州 成都