你針對這個表寫個實體類,寫出它的dao,然后取出所有數(shù)據(jù)放到List,把list放到request.setAttribute("all");在請求到的頁面中用getAttribute取出,然后用js寫DOM模型表示出來,js中的引用可以直接使用java的數(shù)據(jù),例如:List l = (List)request.getAttribute("all");for(int i==0;i實體類 a = ( 實體類)l.get(i);%>var url = }%>DOM模型可以使js更好的控制我們想要實現(xiàn)的效果。
package tree; import java.util.LinkedList; import java.util.List; /** * 功能:把一個數(shù)組的值存入二叉樹中,然后進行3種方式的遍歷 * * 參考資料0:數(shù)據(jù)結構(C語言版)嚴蔚敏 * * 參考資料1:#java * * @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 List 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 LinkedList(); // 將一個數(shù)組的值依次轉換為Node節(jié)點 for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) { nodeList.add(new Node(array[nodeIndex])); } // 對前l(fā)astParentIndex-1個父節(jié)點按照父節(jié)點與孩子節(jié)點的數(shù)字關系建立二叉樹 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); } } /** * 先序遍歷 * * 這三種不同的遍歷結構都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節(jié)點 */ public static void preOrderTraverse(Node node) { if (node == null) return; System.out.print(node.data + " "); preOrderTraverse(node.leftChild); preOrderTraverse(node.rightChild); } /** * 中序遍歷 * * 這三種不同的遍歷結構都是一樣的,只是先后順序不一樣而已 * * @param node * 遍歷的節(jié)點 */ public static void inOrderTraverse(Node node) { if (node == null) return; inOrderTraverse(node.leftChild); System.out.print(node.data + " "); inOrderTraverse(node.rightChild); } /** * 后序遍歷 * * 這三種不同的遍歷結構都是一樣的,只是先后順序不一樣而已 * * @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); } }。
聲明:本網(wǎng)站尊重并保護知識產(chǎn)權,根據(jù)《信息網(wǎng)絡傳播權保護條例》,如果我們轉載的作品侵犯了您的權利,請在一個月內(nèi)通知我們,我們會及時刪除。
蜀ICP備2020033479號-4 Copyright ? 2016 學習鳥. 頁面生成時間:2.976秒