一道面试题

Class cls{
public String clsNo;
public String clsName;
Public String supclass;
}

Class Goods{
publi String goodsNo;
public String goodsName;
public String clsNo;
}
public List clsList内容:
分类代码 分类名称 上级分类
101 yqtl

102 tl 101
103 tztl 102
104 fstl 102

public List goodLis内容
商品编号 名称 所属分类
0001 snfsj 104
0003 cffsj 104
0002 cffsj 104
查出分类代码clsNo 下的所有商品
请问 怎么做的?贴代码最好了

的确是封装树就可以啦上代码你看看 关键是就是遍历数组

public class TreeUtil {
public static ListgetTree(List list){
List tree= new ArrayList();
List parent= new ArrayList();
List chil =new ArrayList();
if(list==null){
return tree;
}
for(Tree ca:list){
if(ca.getLeaf()==0){//取出所有根节点
CTree pt=new CTree();
pt.setId(ca.getId());
pt.setCid(ca.getCid());
pt.setCls(ca.getCls());
pt.setText(ca.getText());
parent.add(pt);
}else{//取出所有子节点
CTree ch=new CTree();
ch.setId(ca.getId());
ch.setCid(ca.getCid());
ch.setCls(ca.getCls());
ch.setText(ca.getText());
ch.setUrl(ca.getUrl());
ch.setLeaf(ca.getLeaf()==1?true:false);
chil.add(ch);
}
}
for(CTree pr:parent){
List child= new ArrayList();
for(CTree cd:chil){
if(pr.getCid()==cd.getCid()){
child.add(cd);
pr.setChildren(child);
}
}
tree.add(pr);
}

return tree;

}
}

package com.byd.entity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Tree implements Serializable{
private String text;
private int id;
private int leaf;
private String cls;
private String url;
private int cid;//类别ID
public Tree(){

}
/**

  • @return the text / public String getText() { return text; } /*
  • @param text the text to set
    */
    public void setText(String text) {
    this.text = text;
    }

    /**

  • @return the cls
    /
    public String getCls() {
    return cls;
    }
    /
    *

  • @param cls the cls to set
    /
    public void setCls(String cls) {
    this.cls = cls;
    }
    /
    *

  • @return the cid
    /
    public int getCid() {
    return cid;
    }
    /
    *

  • @param cid the cid to set
    /
    public void setCid(int cid) {
    this.cid = cid;
    }
    /
    *

  • @return the leaf
    /
    public int getLeaf() {
    return leaf;
    }
    /
    *

  • @param leaf the leaf to set
    /
    public void setLeaf(int leaf) {
    this.leaf = leaf;
    }
    /
    *

  • @return the id
    /
    public int getId() {
    return id;
    }
    /
    *

  • @param id the id to set
    /
    public void setId(int id) {
    this.id = id;
    }
    /
    *

  • @return the url
    /
    public String getUrl() {
    return url;
    }
    /
    *

  • @param url the url to set
    */
    public void setUrl(String url) {
    this.url = url;
    }
    }

建棵树,就好了。 :arrow: