?? nolevelsort.java
字號(hào):
package com.v246;
/*
*無(wú)級(jí)分類(lèi),數(shù)據(jù)庫(kù)(MYSQL)建表如下:
*create table class(Id int(10) not null auto_increment,ParentID int(10) not null,Title varchar(255) not null,ClassDir varchar(255),Flag bit default 0,ChildNum int(10),Target varchar(255),PRIMARY KEY(Id));
*
**/
public class NoLevelSort
{
protected java.util.Collection coll=new java.util.ArrayList();
private StringAnalysis ana=new StringAnalysis();
public String[] reTitle;
public String[] reId;
private String title="";//用于存儲(chǔ)分完類(lèi)之后的欄目名稱(chēng),以xxxx## yyyyyy## ddddd型式存儲(chǔ),用的時(shí)候要用StrngToKen分析出來(lái),然后輸出即可得到
private String id="";//用于存似無(wú)級(jí)分類(lèi)相應(yīng)欄目名稱(chēng)的ID,存儲(chǔ)形似同TITLE
private String [] re=new String[2];//最后的TITLE和ID要存信到這個(gè)數(shù)組中,以便返回
private String [][] rows=null;//一具二維數(shù)組,里面存儲(chǔ)了所有行和列的值
private String rowCount=null;//得到數(shù)據(jù)的總行數(shù)
public NoLevelSort()throws Exception
{
try
{
this.getNoLevelSort();
}
catch(Exception e0)
{
throw new Exception("Error in NoLevelSort.class 22! "+e0.getMessage());
}
for(int i=0;i<reTitle.length;i++)
{
coll.add(new NoLevelSortValues(reTitle[i],reId[i]));
}
}
public java.util.Collection getColl()
{
return coll;
}
public void getNoLevelSort()throws Exception//開(kāi)始無(wú)級(jí)分類(lèi)的分析
{
Connections connTmp=new Connections();
AquGetRows aquGetRows=new AquGetRows();//這個(gè)就是用來(lái)返回以指定名稱(chēng)的表的所有行和列的值的類(lèi)
try
{
rows=aquGetRows.getRows(connTmp.getConnection(),"class");//這是類(lèi)中的方法,調(diào)用方法后就得到了一個(gè)存儲(chǔ)指定表所有行和列的二維數(shù)組,參數(shù)(Connection,String(tableName)
}
catch(Exception e)
{
throw new Exception("error in NoLevelSort.class 43! "+e.getMessage());
}
rowCount=String.valueOf(aquGetRows.getRowsCount());//得到數(shù)據(jù)表部行數(shù)(一共有多少條記錄)
this.CheckList("0",rowCount,"");//開(kāi)始要析取得的數(shù)據(jù),形成無(wú)級(jí)分類(lèi)
// re[0]=title;//存儲(chǔ)到一維數(shù)組以后返回給用戶
// re[1]=id;
// return re;
reTitle=ana.startAnalysis(title,"##");
reId=ana.startAnalysis(id,"##");
}
public String[] getTitle()
{
return reTitle;
}
public String[] getId()
{
return reId;
}
public void CheckList(String ParentID,String cs,String Str1)//參數(shù)說(shuō)明,ParentID:父級(jí)欄目ID(誰(shuí)是它老爸),cs:數(shù)據(jù)庫(kù)中的總記錄(行數(shù)),Str1:父類(lèi)間隔
{
int i=0;//循環(huán)用
int j=0;//結(jié)束遞歸用
int Cs=Integer.parseInt(cs);//將字符串轉(zhuǎn)換成數(shù)字類(lèi)型
String Str2;
for (i=0;i<Integer.parseInt(rowCount);i++)//遍歷數(shù)據(jù)庫(kù)所有記錄
{
if(rows[1][i].equals(ParentID))//如果當(dāng)前記錄ID為父類(lèi)ID
{
//Response.write(Str1)
if (j<Cs-1) //如果下面還有記錄,也就是說(shuō)還有兄弟節(jié)點(diǎn)
Str2="├";
else //如果該ID沒(méi)有兄弟結(jié)點(diǎn),它是最后一個(gè)
Str2="└";
title=title+"##"+Str1+Str2+rows[2][i]; //記錄到Title內(nèi)
// System.out.println(title);
id=id+"##"+rows[0][i];//將當(dāng)前ID記錄到ID內(nèi)
if (rows[4][i].equals("true")) //如果該示記為真,也就是說(shuō)該欄目下面還有子欄目的話
{
if (j<Cs-1) //如果該節(jié)點(diǎn)不是最后一個(gè),也就是說(shuō)沒(méi)有完成所有遍歷
CheckList(rows[0][i],rows[5][i],Str1+"┆");//那么以該結(jié)點(diǎn)的ID加上該節(jié)點(diǎn)的字女?dāng)?shù)進(jìn)行遞歸并加上|表記,
else
CheckList(rows[0][i],rows[5][i],Str1+" ");//關(guān)鍵所在,遞歸調(diào)用 //否則,也就是該結(jié)是新節(jié)點(diǎn),重新對(duì)該節(jié)點(diǎn)進(jìn)行遍歷
}
j=j+1;//每完成一次遍歷,J的值加一,直到完成所有記錄的遍歷
}
}
}
public static void main(String []args)
{
String[] aqu=null;
String[] aqu1=null;
NoLevelSort sort=null;
try
{
sort=new NoLevelSort();
// aqu1=sort.getNoLevelSort();
}
catch(Exception e)
{
}
StringAnalysis ana=new StringAnalysis();
// System.out.println(aqu1[0]);
aqu=sort.getTitle();
for(int y=0;y<aqu.length;y++)
{
System.out.println(aqu[y]);
}
aqu=sort.getId();
for(int y=0;y<aqu.length;y++)
{
System.out.println(aqu[y]);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -