真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼寫出樹狀圖 java 樹狀圖

怎么用Java統(tǒng)計記事本里的詞條,并做成一個樹狀圖

思路:

成都創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元平遙做網(wǎng)站,已為上家服務,為平遙各地企業(yè)和個人服務,聯(lián)系電話:13518219792

首先你要把詞條的數(shù)據(jù)源整理好

然后把數(shù)據(jù)源和你的記事本讀取的數(shù)據(jù)進行比較

最后統(tǒng)計每個詞條出現(xiàn)的次數(shù),并做成樹狀圖

哪位大俠可以幫我寫一段java代碼!是關(guān)于根據(jù)數(shù)據(jù)庫里的數(shù)據(jù)來形成一個動態(tài)樹形圖

這是代碼,你可以自己調(diào)試一下。

數(shù)據(jù)結(jié)構(gòu)如下:

CREATE TABLE dtree (

id int,

pid int,

name varchar(200),

url varchar(200),

title varchar(200),

target varchar(200),

icon varchar(200),

iconopen varchar(200),

opened bit);

為了實現(xiàn)獲取數(shù)據(jù)庫變量功能,需要建立一個DTree類,并編譯生成CLASS文件,放入\WEB-INF\classes文件夾下。

DTree類代碼如下:

package work3;

public class DTree {

private int id;

private int pid;

private String name;

private String url;

private String title;

private String target;

private String icon;

private String iconOpen;

private int opened;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public int getPid() {

return pid;

}

public void setPid(int pid) {

this.pid = pid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getTarget() {

return target;

}

public void setTarget(String target) {

this.target = target;

}

public String getIcon() {

return icon;

}

public void setIcon(String icon) {

this.icon = icon;

}

public String getIconOpen() {

return iconOpen;

}

public void setIconOpen(String iconOpen) {

this.iconOpen = iconOpen;

}

public int getOpened() {

return opened;

}

public void setOpened(int opened) {

this.opened = opened;

}

}

work3.jsp代碼如下:

%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%

%@ page import="java.sql.*"%

jsp:useBean id='settree' scope="application" class="work3.DTree" /

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

""

html

head

meta http-equiv="Content-Type" content="text/html; charset=GB18030"

link rel="StyleSheet" href="dtree.css" type="text/css" /

script type="text/javascript" src="dtree.js"/script

titledTree in MySQL/title

/head

body

h2

Example

/h2

div class="dtree"

p

a href="javascript: d.openAll();"open all/a |

a href="javascript: d.closeAll();"close all/a

/p

script type="text/javascript"

!--

d = new dTree('d');

%

//驅(qū)動程序名

String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

//數(shù)據(jù)庫用戶名

String userName = "sa";

//密碼

String userPwd = "1";

//數(shù)據(jù)庫名

String dbName = "master";

//表名

String tableName = "dtree";

//連接字符串

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;

//加載驅(qū)動

Class.forName(driverName).newInstance();

//連接數(shù)據(jù)庫

java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);

//得到Statement實例

java.sql.Statement statement = conn.createStatement();

//查詢數(shù)據(jù)

String sql = "select * from " + tableName;

//返回結(jié)果

java.sql.ResultSet rs = statement.executeQuery(sql);

//獲取變量

while (rs.next()) {

settree.setId(rs.getInt(1));

settree.setPid(rs.getInt(2));

settree.setName(rs.getString(3));

settree.setUrl(rs.getString(4));

settree.setTitle(rs.getString(5));

settree.setTarget(rs.getString(6));

settree.setIcon(rs.getString(7));

settree.setIconOpen(rs.getString(8));

settree.setOpened(rs.getInt(9));

if(settree.getPid()==0)

settree.setOpened(1);

%

d.add(%=settree.getId()%,%=settree.getPid()%,'%=settree.getName()%','%=settree.getUrl()%','%=settree.getTitle()%','%=settree.getTarget()%','','',%=settree.getOpened()%);

%

}

%

document.write(d);

//--

/script

/div

/body

/html

請問這個樹狀圖在Java中應該怎么做?

打個比方

可以寫一個Tree的類

public class Tree extends BaseDomain {

private String id;

private String key;

private String icon;

private String title;

private String value;

private String text;

private String code;

private String mtype;

private String type;

/**

* 部門的特殊個別字段

* 1.部門 / 2.崗位 的編碼

*/

private String number;

private Double order;

private Double sort;

private String href;

private String component;

private ListTreeT children;

private String parentId;

private boolean hasParent = false;

private boolean hasChildren = false;

private Date createTime;

private Date modifyTime;

public void initChildren(){

this.children = new ArrayList();

}

}

在寫一個工具類

public class TreeUtil {

protected TreeUtil() {

}

private final static String TOP_NODE_ID = "0";

/**

* 用于構(gòu)建菜單

*

* @param nodes nodes

* @param T ? T

* @return T TreeT

*/

public static T TreeT build(ListTreeT nodes) {

if (nodes == null) {

return null;

}

ListTreeT topNodes = new ArrayList();

nodes.forEach(node - {

String pid = node.getParentId();

if (pid == null || TOP_NODE_ID.equals(pid)) {

topNodes.add(node);

return;

}

for (TreeT n : nodes) {

String id = n.getId();

if (id != null id.equals(pid)) {

if (n.getChildren() == null)

n.initChildren();

n.getChildren().add(node);

node.setHasParent(true);

n.setHasChildren(true);

n.setHasParent(true);

return;

}

}

if (topNodes.isEmpty())

topNodes.add(node);

});

TreeT root = new Tree();

root.setId("0");

root.setParentId("");

root.setHasParent(false);

root.setHasChildren(true);

root.setChildren(topNodes);

root.setText("root");

return root;

}

}

寫完了這兩個在寫業(yè)務層

一個構(gòu)建書的方法

private void buildTrees(List trees, List menus, List ids) {

menus.forEach(menu - {

ids.add(menu.getId().toString());

Tree tree = new Tree();

tree.setId(menu.getId().toString());

tree.setKey(tree.getId());

tree.setParentId(menu.getParentId().toString());

tree.setText(menu.getName());

tree.setTitle(tree.getText());

tree.setIcon(menu.getIcon());

tree.setComponent(menu.getComponent());

tree.setCreateTime(menu.getCreateTime());

tree.setCreateTime(menu.getCreateTime());

tree.setHref(menu.getHref());

tree.setSort(menu.getSort());

tree.setCode(menu.getCode());

tree.setMtype(menu.getMtype());

trees.add(tree);

});

}

MapString, Object result = new HashMap();

ListDept depts = findDepts(dept, request);

ListTreeDept trees = new ArrayList();

buildTrees(trees, depts);

TreeDept deptTree = TreeUtil.build(trees);

result.put("rows", deptTree);

result.put("total", depts.size());

如何用Java實現(xiàn)樹形結(jié)構(gòu)???

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一個數(shù)組的值存入二叉樹中,然后進行3種方式的遍歷

*

* 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴蔚敏

*

* 參考資料1:

*

* 參考資料2:

*

* @author ocaicai@yeah.net @date: 2011-5-17

*

*/

public class BinTreeTraverse2 {

private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

private static ListNode nodeList = null;

/**

* 內(nèi)部類:節(jié)點

*

* @author ocaicai@yeah.net @date: 2011-5-17

*

*/

private static class Node {

Node leftChild;

Node rightChild;

int data;

Node(int newData) {

leftChild = null;

rightChild = null;

data = newData;

}

}

public void createBinTree() {

nodeList = new LinkedListNode();

// 將一個數(shù)組的值依次轉(zhuǎn)換為Node節(jié)點

for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {

nodeList.add(new Node(array[nodeIndex]));

}

// 對前l(fā)astParentIndex-1個父節(jié)點按照父節(jié)點與孩子節(jié)點的數(shù)字關(guān)系建立二叉樹

for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {

// 左孩子

nodeList.get(parentIndex).leftChild = nodeList

.get(parentIndex * 2 + 1);

// 右孩子

nodeList.get(parentIndex).rightChild = nodeList

.get(parentIndex * 2 + 2);

}

// 最后一個父節(jié)點:因為最后一個父節(jié)點可能沒有右孩子,所以單獨拿出來處理

int lastParentIndex = array.length / 2 - 1;

// 左孩子

nodeList.get(lastParentIndex).leftChild = nodeList

.get(lastParentIndex * 2 + 1);

// 右孩子,如果數(shù)組的長度為奇數(shù)才建立右孩子

if (array.length % 2 == 1) {

nodeList.get(lastParentIndex).rightChild = nodeList

.get(lastParentIndex * 2 + 2);

}

}

/**

* 先序遍歷

*

* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已

*

* @param node

* 遍歷的節(jié)點

*/

public static void preOrderTraverse(Node node) {

if (node == null)

return;

System.out.print(node.data + " ");

preOrderTraverse(node.leftChild);

preOrderTraverse(node.rightChild);

}

/**

* 中序遍歷

*

* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已

*

* @param node

* 遍歷的節(jié)點

*/

public static void inOrderTraverse(Node node) {

if (node == null)

return;

inOrderTraverse(node.leftChild);

System.out.print(node.data + " ");

inOrderTraverse(node.rightChild);

}

/**

* 后序遍歷

*

* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已

*

* @param node

* 遍歷的節(jié)點

*/

public static void postOrderTraverse(Node node) {

if (node == null)

return;

postOrderTraverse(node.leftChild);

postOrderTraverse(node.rightChild);

System.out.print(node.data + " ");

}

public static void main(String[] args) {

BinTreeTraverse2 binTree = new BinTreeTraverse2();

binTree.createBinTree();

// nodeList中第0個索引處的值即為根節(jié)點

Node root = nodeList.get(0);

System.out.println("先序遍歷:");

preOrderTraverse(root);

System.out.println();

System.out.println("中序遍歷:");

inOrderTraverse(root);

System.out.println();

System.out.println("后序遍歷:");

postOrderTraverse(root);

}

}

java如何輸出樹型圖

你是要那種的。。。就是選擇一個目錄。

然后把下面的所有都列出來,

就可以了嗎

import java.io.File;

public class Test {

public static void main(String[] args) {

File file=new File("C:/");

viewTree(file,index);

}

static int index=0;

private static void viewTree(File file,int index) {

String space="";

for(int j=0;jindex;j++){

space += " ";

}

File [] files= file.listFiles();

for(int i=0;ifiles.length;i++){

File f=files[i];

if(f.isDirectory()){

index++;

System.out.println(space+"+"+f.getName());

viewTree(f,index);

}else{

System.out.println(space+"-"+f.getName());

}

}

}

}


本文題目:java代碼寫出樹狀圖 java 樹狀圖
路徑分享:http://weahome.cn/article/hhghjg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部