本文目錄一覽:
- 1、遞歸實現java無限極菜單
- 2、java樹級對象遞歸查找子集問題
- 3、用java遞歸方法實現
- 4、JAVA題目:Java題目:用遞歸寫n個元素的子集 static Object[] subs(Object[] chosen, int i, int n)
- 5、在JAVA中什麼是遞歸?有什麼用?
- 6、JAVA遞歸找所有子集
遞歸實現java無限極菜單
說下我個人的做法吧,不考慮任何效率問題,我是在查詢對象的時候,把對象用遞歸方法先封裝成一個集合。就是第一次查的時候,會拿到根,然後就可以開始使用遞歸去把子類提出來,直到沒有兒子。最後只需要把這個集合直接JSONArray 轉成json字符串。丟到前台就行了。
java樹級對象遞歸查找子集問題
package com.demo.dept;
/**
* @author dongbin.yu
* @from 2016-05-06
* @since V1.0
*/
public class Dept {
private int id;
private String name;
private int parentId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public Dept(int id, String name, int parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
}
package com.demo.dept;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author dongbin.yu
* @from 2016-05-06
* @since V1.0
*/
public class DeptTest {
private static ListDept depts = new ArrayList();
static{
depts.add(new Dept(1,”部門1″,0));
depts.add(new Dept(2,”部門2″,1));
depts.add(new Dept(3,”部門3″,1));
depts.add(new Dept(4,”部門4″,1));
depts.add(new Dept(5,”部門5″,2));
depts.add(new Dept(6,”部門6″,3));
depts.add(new Dept(7,”部門7″,2));
depts.add(new Dept(8,”部門8″,2));
depts.add(new Dept(9,”部門9″,1));
depts.add(new Dept(10,”部門10″,5));
}
public static void main(String[] args) {
MapInteger, ListInteger deptMap = new HashMap();
for (Dept dept : depts) {
deptMap.put(dept.getId(),getChildDept(dept.getId()));
}
System.out.println(deptMap);
}
private static ListInteger getChildDept(int id){
ListInteger ids = new ArrayList();
for (Dept dept : depts) {
if(dept.getParentId() == id){
//添加第一次父id符合的
ids.add(dept.getId());
//添加嵌套父id符合的
ids.addAll(getChildDept(dept.getId()));
}
}
Collections.sort(ids);
return ids;
}
}
用java遞歸方法實現
1、遞歸做為一種算法在程序設計語言中廣泛使用,是指函數/過程/子程序在運行過程中直接或間接調用自身而產生的重入現象。
2、遞歸算法一般用於解決三類問題:
1)數據的定義是按遞歸定義的。(Fibonacci(斐波那契)的函數)
2)問題解法按遞歸算法實現。(回溯)
3)數據的結構形式是按遞歸定義的。(樹的遍歷,圖的搜索)
JAVA題目:Java題目:用遞歸寫n個元素的子集 static Object[] subs(Object[] chosen, int i, int n)
遞歸問題都是千篇一律的,將問題分為n類,每一類問題又可以分為n類。
這個問題中,設a為集合,將所有子集合分為兩類,有a[0]的和沒a[0]的。
有a[0]的分為兩類,有a[1]的和沒a[1]的。
。。。
有a[n-2]的分為兩類,有a[n-1]的和沒a[n-1]的。
有a[n-1]的直接返回[a[n-1]]。
沒a[n-1]的直接返回[]。
subs(chosen,i,n){//subs為全部集合,chosen為數組,i為數組索引,從1開始,n為數組長度
if(i1){return;}
if(i==n){//當i為n時,也就是掃描完數組了。
if(!chosen)//如果數組為空,也就是沒有chosen[n-1]的情況
return [];//返回空數組
}else{
return [chosen[i-1]];//否則為有a[n-1]的情況
}
arr1=subs(delete_arr_index(chosen,i-1),i+1,n);//沒chosen[i-1]的情況,當i=1時,也就是求得沒有a[0]的所有集合。
arr2=subs(chosen,i+1,n);//有chosen[i-1]的情況,當i=1時,也就是求得有a[0]的所有集合。
arr=arr1+arr2//合併數組,當i=1時,就是獲得了全部集合。
return arr;
}
//刪除數組中的某個元素
public static void delete_arr_index(str,index) {
//刪除php
Listint list = new ArrayListint();
for (int i=0; istr.length; i++) {
list.add(str[i]);
}
list.remove(index);
int[] newStr = list.toArray(new int[1]);
reuturn newStr;
}
在JAVA中什麼是遞歸?有什麼用?
Java方法遞歸是指在一個方法的內部調用自身的過程,以此類推就是java方法遞歸的理解思想,具體來講就是把規模大的問題轉化為規模小的相似的子問題來解決。在函數實現時,因為解決大問題的方法和解決小問題的方法往往是同一個方法,所以就產生了函數調用它自身的情況。另外這個解決問題的函數必須有明顯的結束條件,這樣就不會產生無限遞歸的情況了。因此,java方法遞歸的兩個條件就是,一通過遞歸調用來縮小問題規模,且新問題與原問題有着相同的形式;二存在一種簡單情境,可以使遞歸在簡單情境下退出。
JAVA遞歸找所有子集
package web;
import java.util.ArrayList;
public class SubsetGenerator
{
int[] indexs = null;
int COUNT = 1;// choose how many to be combination
ArrayListString list = new ArrayListString ();
private String subsets;
public SubsetGenerator( String subsets )
{
this.subsets = subsets;
}
public ArrayListString getSubsets ( int… params )
{
if(params.length == 0)
{
indexs = new int[subsets.length ()];
params = new int[2];
params[0] = 0;
params[1] = -1;
list.add (“”);
}
params[1]++;
if(params[1] COUNT – 1)
{
return list;
}
for( indexs[params[1]] = params[0]; indexs[params[1]] subsets.length (); indexs[params[1]]++ )
{
getSubsets (indexs[params[1]] + 1, params[1]);
if(params[1] == COUNT – 1)
{
String temp = “”;
for( int i = COUNT – 1; i = 0; i– )
{
temp += subsets.charAt (indexs[params[1] – i]);
}
list.add (temp);
}
}
if(COUNT subsets.length () params[0] == 0)
{
COUNT++;
getSubsets (0, -1);
}
return list;
}
}
package web;
import java.util.ArrayList;
import java.util.Collections;
public class SubsetGeneratorTester
{
public static void main ( String[] args )
{
SubsetGenerator generator = new SubsetGenerator (“rum”);
ArrayListString subsets = generator.getSubsets ();
Collections.sort (subsets);
if(!”[, m, r, rm, ru, rum, u, um]”.equals (subsets.toString ()))
{
System.err.println (“Expected: [, m, r, rm, ru, rum, u, um]”);
}
else
{
System.err.println (“Congratulations !”);
System.out.println (“Your result is: ” + subsets);
}
}
}
原創文章,作者:UFSA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/142210.html