08-03
01
无限分类
作者:Java伴侣 日期:2008-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
*
*/
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;
}
}
* @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;
}
}
评论: 0 | 引用: 0 | 查看次数: 600
发表评论