本文目錄一覽:
- 1、java遞歸遍歷某個菜單下的菜單樹
- 2、java list 迭代(遞歸)?
- 3、用數組實現java菜單
- 4、java的遞歸查詢怎麼寫
- 5、使用java遞歸方法遍歷指定目錄下所有子目錄和子文件
- 6、急~~~~JAVA如何輸入字符實現菜單循環!!!
java遞歸遍歷某個菜單下的菜單樹
不太清楚你這個Menu是哪來的類,不過如果上文你的程序能執行的話,說明menu.getChilds()是個集合,應該帶有size()的函數。你可以取出menu.getChilds()的大小,再從頭到尾遍歷它。
int count=menu.getChilds().size();
for(int i=0;icount;i++)
{
showMenu( ((Menu)menu.getChilds().get(i)) , 0 );
//我估計這些children是個list,可以順序遍歷;但也有
//部分可能是set,那樣就得用iterator了。
}
java list 迭代(遞歸)?
要用遞歸
public static void showList(ListMenu allMenu) {
if (allMenu == null || allMenu.size() == 0 ) {
return ;
} else {
IteratorMenu iter = allMenu.iterator() ;
while(iter.hasNext()) {
Menu m = iter.next();
// 打印信息或將信息保存到一個公共集合中
System.out.println(m.getMenuId());
showList(m.getChilds());
}
}
}
public static void main(String[] args) {
// 把所有內容的集合傳入
showList(all);
}
用數組實現java菜單
要用二維數組:
String menultems[][] = {{ “住戶資料”, “繳費狀況” }, { “設施管理”, “設施維護” }, …….};
for (int i = 0; i menu.length; i++) { //用數組創建Menu
JMenu menu = new JMenu(menu[i])
br.add(menu);
for(int k = 0; k=menuItems[i].length; k++){
menu.add(new JMenuItem(menuItems[i][k]));
}
}
java的遞歸查詢怎麼寫
原文在這裡,寫得不錯,樓主可參考下,具體鏈接如下,我只是搬運工!
/**
* 說明方法描述:將list轉為樹tree結構
*
* @param allRrecords
* @return
* @time 2016年5月10日 下午6:00:35
* @author yangdong
*/
public ListRecord useListRecordToTree(ListRecord allRrecords) {
ListRecord listParentRecord = new ArrayListRecord();
ListRecord listNotParentRecord = new ArrayListRecord();
// 第一步:遍歷allRrecords保存所有數據的uuid用於判斷是不是根節點
MapString, String mapAllUuid = new HashMapString, String();
MapString, Record allRecordMap = new HashMapString, Record();
for (Record record : allRrecords) {
mapAllUuid.put(record.getStr(“uuid”), record.getStr(“uuid”));
allRecordMap.put(record.getStr(“uuid”), record);
}
// 第二步:遍歷allRrecords找出所有的根節點和非根節點
if (allRrecords != null allRrecords.size() 0) {
for (Record record : allRrecords) {
if (StringUtil.isBlank(record.getStr(“parent_uuid”))
|| !mapAllUuid.containsKey(record.getStr(“parent_uuid”))) {
listParentRecord.add(record);
} else {
listNotParentRecord.add(record);
}
}
}
// 第三步: 遞歸獲取所有子節點
if (listParentRecord.size() 0) {
for (Record record : listParentRecord) {
// 添加所有子級
record.set(“childs”, this.getTreeChildRecord(listNotParentRecord, record.getStr(“uuid”)));
}
}
return listParentRecord;
}
/**
* 說明方法描述:使list轉換為樹並根據關鍵字和節點名稱過濾
*
* @param allRecords 所有節點
* @param keywords 要過濾的關鍵字
* @param filterFields 要過濾的字段
* @return
* @time 2016年5月19日 下午3:27:32
* @author yangdong
*/
public ListRecord useListRecordToTreeByKeywords(ListRecord allRecords, String keywords, String… filterFields) {
ListRecord listRecord = new ArrayListRecord();
MapString, Record allRecordMap = new HashMapString, Record();
for (Record record : allRecords) {
allRecordMap.put(record.getStr(“uuid”), record);
}
// 遍歷allRrecords找出所有的nodeName和關鍵字keywords相關的數據
if (allRecords != null allRecords.size() 0) {
if (filterFields.length 1) {
for (Record record : allRecords) {
for (String field : filterFields) {
// 比較
if (record.getStr(field).toLowerCase().indexOf(keywords.toLowerCase()) != -1) {
listRecord.add(record);
}
}
}
} else {
for (Record record : allRecords) {
// 比較
if (record.getStr(filterFields[0]).toLowerCase().indexOf(keywords.toLowerCase()) != -1) {
listRecord.add(record);
}
}
}
}
// 查找過濾出來的節點和他們的父節點
listRecord = this.getSelfAndTheirParentRecord(listRecord, new ArrayListRecord(),
new HashMapString, Record(), allRecordMap);
// 將過濾出來的數據變成樹tree結構
listRecord = this.useListRecordToTree(listRecord);
return listRecord;
}
/**
* 說明方法描述:遞歸查詢子節點
*
* @param childList 子節點
* @param parentUuid 父節點id
* @return
* @time 2016年5月10日 下午3:29:35
* @author yangdong
*/
private ListRecord getTreeChildRecord(ListRecord childList, String parentUuid) {
ListRecord listParentRecord = new ArrayListRecord();
ListRecord listNotParentRecord = new ArrayListRecord();
// 遍歷tmpList,找出所有的根節點和非根節點
if (childList != null childList.size() 0) {
for (Record record : childList) {
// 對比找出父節點
if (StringUtil.equals(record.getStr(“parent_uuid”), parentUuid)) {
listParentRecord.add(record);
} else {
listNotParentRecord.add(record);
}
}
}
// 查詢子節點
if (listParentRecord.size() 0) {
for (Record record : listParentRecord) {
// 遞歸查詢子節點
record.set(“childs”, getTreeChildRecord(listNotParentRecord, record.getStr(“uuid”)));
}
}
return listParentRecord;
}
/**
* 說明方法描述:遞歸找出本節點和他們的父節點
*
* @param parentList 根據關鍵字過濾出來的相關節點的父節點
* @param resultList 返回的過濾出來的節點
* @param filterRecordMap 已經過濾出來的節點
* @param allRecordMap 所有節點
* @return
* @time 2016年5月19日 上午9:53:56
* @author yangdong
*/
private ListRecord getSelfAndTheirParentRecord(ListRecord parentList, ListRecord resultList,
MapString, Record filterRecordMap,
MapString, Record allRecordMap) {
// 當父節點為null或者節點數量為0時返回結果,退出遞歸
if (parentList == null || parentList.size() == 0) {
return resultList;
}
// 重新創建父節點集合
ListRecord listParentRecord = new ArrayListRecord();
// 遍歷已經過濾出來的節點
for (Record record : parentList) {
String uuid = record.getStr(“uuid”);
String parent_uuid = record.getStr(“parent_uuid”);
// 如果已經過濾出來的節點不存在則添加到list中
if (!filterRecordMap.containsKey(uuid)) {
listParentRecord.add(record);// 添加到父節點中
filterRecordMap.put(uuid, record);// 添加到已過濾的map中
allRecordMap.remove(uuid);// 移除集合中相應的元素
resultList.add(record);// 添加到結果集中
}
// 找出本節點的父節點並添加到listParentRecord父節點集合中,並移除集合中相應的元素
if (StringUtil.isNotBlank(parent_uuid)) {
Record parentRecord = allRecordMap.get(parent_uuid);
if (parentRecord != null) {
listParentRecord.add(parentRecord);
allRecordMap.remove(parent_uuid);
}
}
}
// 遞歸調用
getSelfAndTheirParentRecord(listParentRecord, resultList, filterRecordMap, allRecordMap);
return resultList;
}
[java] view plain copy
//示例
[java] view plain copy
/**
* 說明方法描述:遞歸查詢所有權限
*
* @param keyword
* @param is_deleted
* @return
* @time 2016年5月10日 下午3:47:50
* @author yangdong
*/
public ListRecord getRecordByKeywordRecursive(String keyword, String is_deleted) {
// 第一步:查詢所有的數據
StringBuffer sql = new StringBuffer(
” select pa.uuid,pa.parent_uuid,pa.author_code,pa.author_name,pa.is_menu,pa.sort_number,pa.is_enable,pa.menu_icon “);
sql.append(” from s_author pa”);
ListObject params = new ArrayListObject();
sql.append(” where pa.is_deleted=? “);
params.add(is_deleted);
sql.append(” order by pa.sort_number asc “);
ListRecord allRrecords = Db.use(AppConst.DB_DATASOURCE_MAIN).find(sql.toString(), ParamUtil.listToArray(params));
[java] view plain copy
//第二步:將list變為樹tree結構
if (StringUtil.isNotBlank(keyword)) {
return super.useListRecordToTreeByKeywords(allRrecords, keyword, “author_name”);
} else {
return super.useListRecordToTree(allRrecords);
}
}
使用java遞歸方法遍歷指定目錄下所有子目錄和子文件
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 讀取目錄及子目錄下指定文件名的路徑 並放到一個數組裏面返回遍歷
* @author zdz8207
*
*/
public class FileViewer {
public static void main(String[] args) {
//List arrayList = FileViewer.getListFiles(“d:/com”,”html”,true);
//讀取d:/com下的以java 結尾的文件 如有子目錄,包含之(後綴名為null則為所有文件)
//List arrayList = FileViewer.getListFiles(“d:/com”,”java”,true);
//經試驗,後綴不能不填寫,否則編譯不通過,提示「FileViewer.java:17: 非法的表達式開始」。
//另外後綴為””時的情況需要 增加到IF 里去,否則 後綴為””時,不會顯示所有文件
List arrayList = FileViewer.getListFiles(“d:/com”,””,true);
if(arrayList.isEmpty())
{
System.out.println(“沒有符號要求的文件”);
}
else
{
String message = “”;
message += “符號要求的文件數:” + arrayList.size() + “\r\n”;
System.out.println(message);
for (Iterator i = arrayList.iterator(); i.hasNext();)
{
String temp = (String) i.next();
System.out.println(temp);
message += temp + “\r\n”;
}
//將顯示的文件路徑寫到指定的文件里,若文件不存在,則提示IO異常
//java.io.FileNotFoundException: d:\ajax\menu.txt (系統找不到指定的路徑。)
//如果 加個文件是否存在的判斷,如不存在就在當前目錄新建一個,則更好。
appendMethod(“d:/menu.txt”,message);
}
}
public static ListString fileList = new ArrayListString();
/**
*
* @param path 文件路徑
* @param suffix 後綴名
* @param isdepth 是否遍歷子目錄
* @return
*/
public static List getListFiles(String path, String suffix, boolean isdepth)
{
File file = new File(path);
return FileViewer.listFile(file ,suffix, isdepth);
}
public static List listFile(File f, String suffix, boolean isdepth)
{
//是目錄,同時需要遍歷子目錄
if (f.isDirectory() isdepth == true)
{
File[] t = f.listFiles();
for (int i = 0; i t.length; i++)
{
listFile(t[i], suffix, isdepth);
}
}
else
{
String filePath = f.getAbsolutePath();
System.out.println(“suffix = “+suffix);
if(suffix ==”” || suffix == null)
{
//後綴名為null則為所有文件
System.out.println(“—————-“);
fileList.add(filePath);
}
else
{
int begIndex = filePath.lastIndexOf(“.”);//最後一個.(即後綴名前面的.)的索引
String tempsuffix = “”;
if(begIndex != -1)//防止是文件但卻沒有後綴名結束的文件
{
tempsuffix = filePath.substring(begIndex + 1, filePath.length());
}
if(tempsuffix.equals(suffix))
{
fileList.add(filePath);
}
System.out.println(“|||||||||||||||||||”);
}
}
return fileList;
}
/**
* 方法追加文件:使用FileWriter
* @param fileName
* @param content
*/
public static void appendMethod(String fileName, String content)
{
try
{
//打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content + “\r\n”);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
急~~~~JAVA如何輸入字符實現菜單循環!!!
import java.util.Scanner;
/**
* @author yugi111
*/
public class TestScanner
{
public static void main ( String[] args )
{
String tip_system = “獎客富翁系統”;
String tip_menu = “請選擇菜單: “;
String tip_info = “[ ” + tip_system + ” “;
String tip_continue = “繼續嗎? ( y / n ) : “;
String tip_exit = “系統退出, 謝謝使用 ! “;
String infos = “****歡迎進入” + tip_system + “****\n\t ” +
“1.註冊\n \t 2.登錄\n \t 3.抽獎\n” +
“*****************************”;
System.out.println (infos);
System.out.print (tip_menu);
Scanner scanner = new Scanner (System.in);
int step = 1; // 當前流程 (1: 選擇菜單, 2: 是否退出)
String next = null;
while (scanner.hasNext ())
{
if (1 == step) // 選擇菜單流程
{
if (!(next = scanner.next ().trim ()).matches (“1|2|3”))
{
System.err.print (tip_menu);
}
else
{
String info = “”;
int nextInt = Integer.parseInt (next);
switch (nextInt)
{
case 1:
info = “註冊”;
break;
case 2:
info = “登錄”;
break;
case 3:
info = “抽獎”;
break;
default:
info = “註冊”;
break;
}
System.out.println (tip_info + info + ” ]”);
System.out.print (tip_continue);
step = 2; // 進入是否退出流程
}
}
else if (2 == step) // 是否退出流程
{
if (!(next = scanner.next ().trim ()).matches (“Y|N|y|n”))
{
System.err.print (tip_continue);
}
else
{
if (“n”.equalsIgnoreCase (next))
{
System.out.println (tip_exit);
scanner.close ();
System.exit (0);
}
else
{
System.out.println (“\n” + infos);
System.out.print (tip_menu);
step = 1; // 選擇菜單流程
}
}
}
}
}
}
原創文章,作者:WTXHP,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/315659.html