String可以排序的吧..
24個字母是死的吧...
2個字母的就,比了第一個,再比第二個.....
這個思路如何?
『貳』 java中如何建立一個java樹,請詳解
importjava.awt.*;
importjavax.swing.*;
classTreeDemoextendsJFrame
{
publicTreeDemo()
{
setSize(400,300);
setTitle("演示怎樣使用JTree");
show();
jscrollPanejPanel=newJScrollPane();
getContentPane().add(jPanel);
JTreejtree=newJTree();
jPanel.getViewport().add(jtree,null);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
publicclassExample5_25
{
publicstaticvoidmain(String[]args)
{
TreeDemoframe=newTreeDemo();
}
}
其中JScrollPane是一個帶滾動條的面板類。
將對象加入到帶滾動條的面板類中,在將已建的數放入到其中。
就可建立一個系統默認的樹結構。
『叄』 JAVA數據結構關於查找樹的節點的代碼分析
你好,遞歸調用簡單的說就是自己無限的調用自己的下一級,也就是說父節點和子節點擁有相同的屬性和方法,所以在處理玩當前節點也就是父節點的時候,就調用處理自身下面的子節點,類似於文件夾和文件,文件夾相對於當前目錄來說是父節點,而他裡面的子文件夾就相當於子節點,但是處理子文件夾的時候,子文件夾就成了父節點,一次遞歸處理完畢
希望可以幫助到你
『肆』 java編打出5行聖誕樹,求教每一步詳細思想。下面是代碼
按照你的要求加詳細注釋的聖誕樹Java程序如下:(編程思想在注釋中說明)
publicclassShengDanShu2{
//這個程序的編程思想是利用對for循環變數i的控制達到一層循環代替雙層循環的目的
publicstaticvoidmain(String[]args){
intn=5;//初始化列印聖誕樹層數變數n
inta=0;//初始化列印前置空格數變數a
intb=0;//初始化列印星號數變數b
for(inti=1;i<=n;i++){//列印n層聖誕樹
if(a!=(n-i)){//如果前置空格數不等於n-i
System.out.print("");//列印一個空格
a++;//前置空格數加一
i=i-1;//i變數減一目的是固定住i變數不變直到a==n-i
}elseif(b!=(2*i-1)){//如果星號數不等於2*i-1
System.out.print("*");//列印一個星號
b++;//星號數加一
i=i-1;//i變數減一目的是固定住i變數不變直到b==2*i-1
}elseif(a==(n-i)&&b==(2*i-1)){//當以上兩個條件都滿足時,換行初始化a和b為0
System.out.println();//列印換行
a=0;//對新的一行重新初始化前置空格數變數a
b=0;//對新的一行重新初始化列印星號數變數b
//這里沒有控制for循環的i變數減一,因為這時i變數加一,開始新一行。
}
}
}
}運行結果:
 *
 ***
 *****
*******
*********
『伍』 哪位大俠可以幫我寫一段java代碼!是關於根據資料庫里的數據來形成一個動態樹形圖
這是代碼,你可以自己調試一下。
數據結構如下:
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);
為了實現獲取資料庫變數功能,需要建立一個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"
"http://www.w3.org/TR/html4/loose.dtd">
<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>
                <title>dTree 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');
                <%
                        //驅動程序名
                        String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
                        //資料庫用戶名
                        String userName = "sa";
                        //密碼
                        String userPwd = "1";
                        //資料庫名
                        String dbName = "master";
                        //表名
                        String tableName = "dtree";
                        //連接字元串
                        String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;
       //載入驅動
                        Class.forName(driverName).newInstance();
       //連接資料庫
                        java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);
       //得到Statement實例
                        java.sql.Statement statement = conn.createStatement();
       //查詢數據
                        String sql = "select * from " + tableName;
       //返回結果
                        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實現二叉樹. (求源碼,要求簡練、易懂。非常滿意會額外加分)
import java.util.ArrayList;
// 樹的一個節點
class TreeNode {
    Object _value = null; // 他的值
    TreeNode _parent = null; // 他的父節點,根節點沒有PARENT
    ArrayList _childList = new ArrayList(); // 他的孩子節點
    public TreeNode( Object value, TreeNode parent ){
        this._parent = parent;
        this._value = value;
    }
    public TreeNode getParent(){
        return _parent;
    }
    public String toString() {
        return _value.toString();
    }
}
public class Tree {
    // 給出寬度優先遍歷的值數組,構建出一棵多叉樹
    // null 值表示一個層次的結束
    // "|" 表示一個層次中一個父親節點的孩子輸入結束
    // 如:給定下面的值數組:
    // { "root", null, "left", "right", null }
    // 則構建出一個根節點,帶有兩個孩子("left","right")的樹
    public Tree( Object[] values ){
        // 創建根
        _root = new TreeNode( values[0], null );
        // 創建下面的子節點
        TreeNode currentParent = _root; // 用於待創建節點的父親
        //TreeNode nextParent = null;
        int currentChildIndex = 0; // 表示 currentParent 是他的父親的第幾個兒子
        //TreeNode lastNode = null; // 最後一個創建出來的TreeNode,用於找到他的父親
        for ( int i = 2; i < values.length; i++ ){
            // 如果null ,表示下一個節點的父親是當前節點的父親的第一個孩子節點
            if ( values[i] == null ){
                currentParent = (TreeNode)currentParent._childList.get(0);
                currentChildIndex = 0;
                continue;
            }
            // 表示一個父節點的所有孩子輸入完畢
            if ( values[i].equals("|") ){
                if ( currentChildIndex+1 < currentParent._childList.size() ){
                    currentChildIndex++;
                    currentParent = (TreeNode)currentParent._parent._childList.get(currentChildIndex);
                }
                continue;
            }
            TreeNode child = createChildNode( currentParent, values[i] );
        }
    }
    TreeNode _root = null;
    public TreeNode getRoot(){
        return _root;
    }
/**
    // 按寬度優先遍歷,列印出parent子樹所有的節點
    private void printSteps( TreeNode parent, int currentDepth ){
        for ( int i = 0; i < parent._childList.size(); i++ ){
            TreeNode child = (TreeNode)parent._childList.get(i);
            System.out.println(currentDepth+":"+child);
        }
        if ( parent._childList.size() != 0 )    System.out.println(""+null);// 為了避免葉子節點也會列印null
        //列印 parent 同層的節點的孩子
        if ( parent._parent != null ){ // 不是root
            int i = 1;
            while ( i < parent._parent._childList.size() ){// parent 的父親還有孩子
                TreeNode current = (TreeNode)parent._parent._childList.get(i);
                printSteps( current, currentDepth );
                i++;
            }
        }
// 遞歸調用,列印所有節點
        for ( int i = 0; i < parent._childList.size(); i++ ){
            TreeNode child = (TreeNode)parent._childList.get(i);
            printSteps( child, currentDepth+1 );
        }
    }
    // 按寬度優先遍歷,列印出parent子樹所有的節點
    public void printSteps(){
        System.out.println(""+_root);
        System.out.println(""+null);
        printSteps(_root, 1 );
    }**/
    // 將給定的值做為 parent 的孩子,構建節點
    private TreeNode createChildNode( TreeNode parent, Object value ){
        TreeNode child = new TreeNode( value , parent );
        parent._childList.add( child );
        return child;
    }
    public static void main(String[] args) {
        Tree tree = new Tree( new Object[]{ "root", null,
                              "left", "right", null,
                              "l1","l2","l3", "|", "r1","r2",null } );
        //tree.printSteps();
        System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(0) );
        System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(1) );
        System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(2) );
        System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(0) );
        System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(1) );
    }
}
看一下吧!這是在網上找的一個例子!看對你有沒有幫助!
『柒』 java 後台處理 list 為樹HTML源碼
構造treelist的過程可以使用遞歸查詢獲取,表結構如下
create table TB_TREE
(
id NUMBER not null,
name VARCHAR2(50),
parentId NUMBER //父節內點
)
Postgre Sql:
select 語句如下,oracle可以用 start with connect by prior實現類似功容能
WITH RECURSIVE r AS (
SELECT