java 对树结构的实现(优化)
package blue;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class BlueTreeOptimTest extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTree tree;
public BlueTreeOptimTest(){
this.createTree();
this.add(tree);
//设定窗口的长和宽
this.setSize(500,700);
TreeNode root = (TreeNode) tree.getModel().getRoot();
this.expandTree(tree, new TreePath(root));
}
public static void main(String[] args) {
JFrame frame = new BlueTreeOptimTest();
frame.setVisible(true);
}
/**获得树的所有数据*/
public void createTree(){
DefaultMutableTreeNode rootTree=new DefaultMutableTreeNode("/");
tree=new JTree(rootTree);
//定义字符串接收项目ID
String str1="BB11-AAMDP-C01-B034T";
String str2="BB11-AAMDP-C01-B324T";
String str3="BB12-11MDP-H00-XXXXT";
String str4="BB12-ASCOP-M00-XXXXT";
String str5="BB13-ABFCX-C01-A597T";
String str6="BB13-ABFCP-M00-XXXXT";
String str7="BB11-AAMDP-C01-B034TXXXXT";
String str8="BB11-AAMDP-C01-B324TA597T";
//加工项目ID为去掉-的字符串
String replaceStr1=str1.replace("-", "");
String replaceStr2=str2.replace("-", "");
String replaceStr3=str3.replace("-", "");
String replaceStr4=str4.replace("-", "");
String replaceStr5=str5.replace("-", "");
String replaceStr6=str6.replace("-", "");
String replaceStr7=str7.replace("-", "");
String replaceStr8=str8.replace("-", "");
//创建List存放加工后的数据
List<String> strList=new ArrayList<String>();
//添加加工后结果字符串
strList.add(replaceStr1);
strList.add(replaceStr2);
strList.add(replaceStr3);
strList.add(replaceStr4);
strList.add(replaceStr5);
strList.add(replaceStr6);
strList.add(replaceStr7);
strList.add(replaceStr8);
//创建数组存放步长
int footLengthArr[]={4,2,1,2,1,2};
//设定每次的起始位置
int startPosition=0;
//设定每次的结束位置
int endPosition=0;
//查询是否存在对应子节点的标记
int checkExistFlag=0;
//父节点
DefaultMutableTreeNode parentTree=null;
if(strList!=null&&strList.size()>0){
for(int i=0;i<strList.size();i++){
for(int k=0;k<footLengthArr.length+1;k++){
startPosition=k<1?0:endPosition;
//获取结束位置,最后一位单独获取,防止出现字符串长度不相等时有异常发生
endPosition=k<footLengthArr.length?startPosition+footLengthArr[k]:strList.get(i).length();
//查询是否存在对应子节点的标记
checkExistFlag=0;
//获取上一轮的父节点
parentTree=k<1?rootTree:parentTree;
//创建子节点
DefaultMutableTreeNode childTree=new DefaultMutableTreeNode(strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<parentTree.getChildCount();j++){
if(parentTree.getChildAt(j).toString().equals(childTree.toString())){
//重置父节点为已存在的有相同内容的节点
parentTree=(DefaultMutableTreeNode)parentTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
parentTree.add(childTree);
//重置父节点为最新添加的子节点
parentTree=childTree;
}
}
}
}else{
System.out.println("请确认项目ID是否存在");
}
}
/**设置树一开始是全部展开的*/
private void expandTree(JTree tree, TreePath parent) {
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandTree(tree, path);
}
}
tree.expandPath(parent);
}
}
效果如下: