java 对象 (树) 的转换 (目录) JSON 节点。

来源:互联网 发布:wifi跑字典软件 编辑:程序博客网 时间:2024/05/18 10:10

使用java 创建一个树对象,和遍历树的方法。

这个类是使用面向对象的方式,使用对象组装了一个树。然后使用遍历出来,详情看代码。

package cn.com.chx.website.util;import java.util.ArrayList;import java.util.List;/** * <p> * Title:  存储栏目对象 * </p> * <p> * Description: 栏目对象实体 * </p> * <p> * Copyright: Copyright 2015 普巴软件有限公司. All rights reserved. * </p> * <p> * Company: 普巴软件有限公司 * </p> *  * @author WangXuDong * @date 2015年10月13日 */public class CatalogTree { public static void main(String[] args) {//String string = shuju[i];//System.out.println(string);//获取所有的一级菜单//String[] shuju1 = new String[]{"004","005","003"};CatalogTreeEntity cte = new CatalogTreeEntity(); //保存生成的树对象CatalogTreeEntity ce004 = new CatalogTreeEntity();  //服装对象ce004.setId("1");ce004.setCaId("服装");cte = getCataTree(ce004,1);   //查找004开头对象的子栏目树ideaCataTree(cte);   //遍历栏目树}/** *  获取栏目树对象 * @param ce 栏目树的根节点栏目对象(没有子栏目) * @param level 栏目从根栏目第level开始建树 * @return 返回栏目树对象(含子栏目) */public static CatalogTreeEntity getCataTree(CatalogTreeEntity ce,int level){level++;List<CatalogTreeEntity> ctes = getSubCata(ce.getCaId(),level);if(ctes.size()>0){    //ce还有子栏目for (int i = 0; i < ctes.size(); i++) {ce.setSubctes(ctes);getCataTree(ctes.get(i),level);}return ce;}else{return null;}}/** * 获取某个栏目下的子栏目 * @param caid 栏目号 * @param level 等级 * @return 某个栏目的某个等级下的子栏目 */public static List<CatalogTreeEntity> getSubCata(String caid,int level){//在数据中查找以004开头,gradeno 为count的栏目对象即可 以集合或数组方式返回List<CatalogTreeEntity> subcec = new ArrayList<CatalogTreeEntity>();List<CatalogTreeEntity> ctes = getCataDate();for (int i = 0; i < ctes.size(); i++) {String s = ctes.get(i).getCaId();String[] ss = s.split("-");String scaid = ss[0];if(ss.length==level){for (int j = 1; j < level-1; j++) {scaid = scaid + "-" + ss[j];}if(scaid.equals(caid)){CatalogTreeEntity cata = new CatalogTreeEntity();cata.setCaId(s);  //获取到的数据放进去cata.setLevel(level);subcec.add(cata);}}}return  subcec;}/** * 从数据库获取所有的号赋给栏目对象,以栏目对象的List方式返回 * @return 所有的栏目(只含有栏目号的栏目对象) */public static List<CatalogTreeEntity> getCataDate(){String[] shuju = {"服装","服装-上衣","服装-上衣-大人","服装-下衣","服装-上衣-小孩","服装-帽子","服装-下衣-鞋子","服装-下衣-鞋子-运动鞋","服装-下衣-鞋子-球鞋","004-002-002","004-003-001","004-002-001-001","005","005-001","005-002","005-002-003","003","003-001","003-002"};List<CatalogTreeEntity> cec = new ArrayList<CatalogTreeEntity>();for (int i = 0; i < shuju.length; i++) {CatalogTreeEntity cte = new CatalogTreeEntity();cte.setCaId(shuju[i]);cec.add(cte);}return cec;}/** * 遍历栏目树 */public static void ideaCataTree(CatalogTreeEntity cte){if(cte.getSubctes()!=null){for (int i = 0; i < cte.getSubctes().size(); i++) {CatalogTreeEntity recte =new  CatalogTreeEntity();recte = cte.getSubctes().get(i);if(recte!=null){String tabStr = "";int c = recte.getLevel()-1;for (int j = 0; j < c; j++) {tabStr +="---";}System.out.println(tabStr+recte.getCaId());ideaCataTree(recte);}}}}}
这是一个树对象,这个对象中的一个集合字段调用了自身。
package cn.com.chx.website.util;import java.io.Serializable;import java.util.List;/** * <p> * Title:  * </p> * <p> * Description:  * </p> * <p> * Copyright: Copyright 2015 普巴软件有限公司. All rights reserved. * </p> * <p> * Company: 普巴软件有限公司 * </p> *  * @author WangXuDong * @date 2015年10月13日 */public class CatalogTreeEntity implements Serializable {/** *  */private static final long serialVersionUID = 1075366343031430593L;private String id;private String caId;private String caName;private int level;private List<CatalogTreeEntity> subctes;public List<CatalogTreeEntity> getSubctes() {return subctes;}public void setSubctes(List<CatalogTreeEntity> subctes) {this.subctes = subctes;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getCaId() {return caId;}public void setCaId(String caId) {this.caId = caId;}public String getCaName() {return caName;}public void setCaName(String caName) {this.caName = caName;}public int getLevel() {return level;}public void setLevel(int level) {this.level = level;}}


0 0
原创粉丝点击