08-03
01

无限分类

package com.tree.model;

/**
* @author wolf
*
*/
public class TreeModel {

    private int classid;

    private int parentid;

    private String name;

    private int level;

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public TreeModel(int classid, int parentid, String name) {
        this.classid = classid;
        this.parentid = parentid;
        this.name = name;
    }

    public TreeModel() {
        // TODO Auto-generated constructor stub
    }

    public int getClassid() {
        return classid;
    }

    public void setClassid(int classid) {
        this.classid = classid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getParentid() {
        return parentid;
    }

    public void setParentid(int parentid) {
        this.parentid = parentid;
    }

}



/**
* @author wolf
* 无限分类
* 本例子是一个示例程序,只有获取分类的功能,对于增加和修改没有实现,没有用到数据库.
*/
package com.tree.bean;

import java.util.ArrayList;
import java.util.Iterator;

import com.tree.model.TreeModel;

/**
* @author wolf
*
*/
public class Tree {

    public static void main(String[] args) {
        TreeModel[] ts = init();
        
        TreeModel tss = new TreeModel();
        
        ArrayList list = new ArrayList();
        
        list = getAll(0, ts, 0, new ArrayList());
        
        Iterator it = list.iterator();
        while (it.hasNext()) {
            tss = (TreeModel) it.next();
            for (int k = 0; k < tss.getLevel(); k++) {
                System.out.print(" ");
            }
            System.out.println(tss.getName());
        }
    }

    /* 初始化,读取数据,并返回数组 */
    public static TreeModel[] init() {
        ArrayList list = new ArrayList();
        list.add(new TreeModel(5, 4, "东软"));
        list.add(new TreeModel(6, 5, "移动互联网"));
        list.add(new TreeModel(7, 3, "望京"));
        list.add(new TreeModel(9, 1, "辽宁"));
        list.add(new TreeModel(10, 9, "大连"));
        list.add(new TreeModel(11, 10, "东软"));
        list.add(new TreeModel(12, 11, "0317班"));
        list.add(new TreeModel(0, -1, "世界"));
        list.add(new TreeModel(1, 0, "中国"));
        list.add(new TreeModel(2, 1, "北京"));
        list.add(new TreeModel(3, 2, "朝阳区"));
        list.add(new TreeModel(4, 1, "沈阳"));


        TreeModel[] ts = new TreeModel[list.size()];
        list.toArray(ts);
        return ts;
    }

    /* 通过classid和数组,返回该classid下的一级分类数组 */
    public static TreeModel[] isHave(int id, TreeModel[] ts) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < ts.length; i++) {
            if (ts[i].getParentid() == id) {
                list.add(ts[i]);
            }
        }
        TreeModel[] tss = new TreeModel[list.size()];
        list.toArray(tss);
        return tss;
    }

    /* 通过classid得到该classid的一个TreeModel对象
     * 循环遍历得到
     * */
    public static TreeModel getTreeByid(int id, TreeModel[] ts) {
        TreeModel tss = new TreeModel();
        for (int i = 0; i < ts.length; i++) {
            if (ts[i].getClassid() == id) {
                tss = ts[i];
            }
        }
        return tss;
    }

    /*
     * 获取所有分类
     * 参数id:这个id是classid
     * 参数ts:所有分类的对象集合
     * 参数level:分类的层次级别
     * 参数list:递归到的分类信息存放到该list中
     */
    static public ArrayList getAll(int id, TreeModel[] ts, int level,
            ArrayList list) {
        level++;
        TreeModel[] k = isHave(id, ts);
        if (id == 0) {
            list.add(getTreeByid(id, ts));
        }
        if (k.length > 0) {
            for (int t = 0; t < k.length; t++) {
                k[t].setLevel(level);
                list.add(k[t]);
                getAll(k[t].getClassid(), ts, level, list);
            }
        }
        return list;
    }
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 无限 分类
相关日志:
评论: 0 | 引用: 0 | 查看次数: 598
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭