導航:首頁 > 源碼編譯 > 編程實現子集遍歷演算法

編程實現子集遍歷演算法

發布時間:2022-06-08 00:13:18

A. 關於用java遍歷集合的演算法

public static void main(String[] args) {
List<A> list=new ArrayList<A>();
list.add(new A(1,1,0));
list.add(new A(2,2,1));
list.add(new A(3,3,1));
list.add(new A(4,4,2));
list.add(new A(5,5,2));
list.add(new A(6,6,3));
list.add(new A(7,7,3));
list.add(new A(8,8,4));
list.add(new A(9,9,5));
diayong(list,2);
}
private static void diayong(List<A>list,int i) {
// TODO Auto-generated method stub
for (A a : list) {
if(a.did==i){
System.out.println(a.id);
diayong(list,a.id);
}
}
}
輸出結果 4 8 5 9 先找到等於2的第一次 4 然後找等於4的 8 然後沒有了 再循環找到等於 2的 5 然後找到9 以此類推 不知道 是不是你要的

B. 遍歷子元素怎麼做

遍歷子元素:

jquery中遍歷子元素,可以利用 $("#ul_tags>dl") 這種需要是直接的子節點,或 $("div#ul_tags:dl") 也是可以的,也可以直接使用$("div#ul_tags a")的形式。

C. 如何遍歷一個集合的所以子集

一個集合中所有元素在它的子集中只有兩種情況:存在或者不存在,所以在列舉子集的時候可以依次列過來,比如用樹狀圖

比如(1,2,3,)這個集合

首先1存在是一組

1不存在是一組

每組下面2的存在與否又有兩種情況,如圖所示,每一層,只討論一個數是否存在

D. Java遞歸遍歷集合

首先把這張表的所有數據查詢出來放到一個集合中(集合為List1),然後遍歷這個集合,先根據集合List1中的父編號查詢到一個集合 ,這樣把list1遍歷的對象和根據父編號查詢到的集合構建問一個對象放到集合中就可以達到你的目的了,一下為代碼說明:

import java.util.List;
public class dto1 {
private things thing;//當前編號得到的對象
private List list;//父編號查詢到的集合
public things getThing() {
return thing;
}
public void setThing(things thing) {
this.thing = thing;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
*********************************************************************************

public class things {

private int number;
private String name;
private int parentnumber;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentnumber() {
return parentnumber;
}
public void setParentnumber(int parentnumber) {
this.parentnumber = parentnumber;
}
}
**************************************************************************

import java.util.ArrayList;
import java.util.List;
public class action {

public static void main(String[] args) {

List getlist=new ArrayList();
List list=new ArrayList();//list為你根據編號查詢出來的集合

for(int i=0;i<list.size();i++)
{
things thing=(things)list.get(i);

int parentbumber=thing.getParentnumber();//得到父編號
List li=new ArrayList();//此li為根據父編號parentbumber查詢到的集合
dto1 dto=new dto1();
dto.setThing(thing);
dto.setList(li);
list.add(dto);//把當前編號的對象和父編號得到的集合構建成一個dto放到最終你要的list中
}

}
}

E. 編寫演算法實現對二叉樹進行按層次遍歷

不明白樓主對於N叉樹向二叉樹的轉換演算法有沒有了解,你的問題取了這個演算法的子集,即對於N=2的具體問題的轉換。
對於N叉樹,可以建立一個二叉樹,左子樹為N叉樹的最左子節點,右子樹為同級節點。然後左序遍歷新樹就好。你去查查演算法設計或者數據結構的書,有完整源碼

F. 在java中集合的遍歷是怎樣遍歷的

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
//集合遍歷
public class TestHashSet{
public static void main(String[] args) {
HashSet h = new HashSet();
h.add("1st");
h.add("2nd");
h.add(new Integer(3));
h.add(new Double(4.0));
h.add("2nd"); // 重復元素, 未被加入
h.add(new Integer(3)); // 重復元素,未被加入
h.add(new Date());
System.out.println("開始: size=" + h.size());
Iterator it = h.iterator();
while(it.hasNext()){
Object o = it.next();
System.out.println(o);
}
}
}

G. 用C語言編程實現圖的遍歷演算法

圖的遍歷是指按某條搜索路徑訪問圖中每個結點,使得每個結點均被訪問一次,而且僅被訪問一次。圖的遍歷有深度遍歷演算法和廣度遍歷演算法,最近阿傑做了關於圖的遍歷的演算法,下面是圖的遍歷深度優先的演算法(C語言程序):
#include<stdio.h>
#include<malloc.h>
#define MaxVertexNum 5
#define m 5
#define TRUE 1
#define NULL 0
typedef struct node
{
int adjvex;
struct node *next;
}JD;
typedef struct EdgeNode
{
int vexdata;
JD *firstarc;
}TD;
typedef struct
{
TD ag[m];
int n;
}ALGRAPH;
void DFS(ALGRAPH *G,int i)
{
JD *p;
int visited[80];
printf("visit vertex:%d->",G->ag[i].vexdata);
visited[i]=1;
p=G->ag[i].firstarc;
while(p)
{
if (!visited[p->adjvex])
DFS(G,p->adjvex);
p=p->next;
}
}
void creat(ALGRAPH *G)
{
int i,m1,j;
JD *p,*p1;
printf("please input the number of graph\n");
scanf("%d",&G->n);
for(i=0;i<G->n;i++)
{
printf("please input the info of node %d",i);
scanf("%d",&G->ag[i].vexdata);
printf("please input the number of arcs which adj to %d",i);
scanf("%d",&m1);
printf("please input the adjvex position of the first arc\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
G->ag[i].firstarc=p;
p1=p;
for(j=2 ;j<=m1;j++)
{
printf("please input the position of the next arc vexdata\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
p1->next=p;
p1=p;
}
}
}
int visited[MaxVertexNum];
void DFSTraverse(ALGRAPH *G)
{
int i;
for(i=0;i<G->n;i++)
visited[i]=0;
for(i=0;i<G->n;i++)
if(!visited[i])
DFS(G,i);
}
int main()
{
ALGRAPH *G;
printf("下面以臨接表存儲一個圖;\n");
creat(G);
printf("下面以深度優先遍歷該圖 \n");
DFSTraverse(G);
getchar();
}

H. 如何在java中實現List集合的遍歷

java中實現List集合的遍歷有三種方法:

方法一:for-each循環

for(Stringattribute:list){
System.out.println(attribute);
}

方法二:for循環

for(inti=0;i<list.size();i++){
system.out.println(list.get(i));
}

方法三:迭代器迭代

Iteratorit=list.iterator();
while(it.hasNext()){
System.ou.println(it.next);
}
閱讀全文

與編程實現子集遍歷演算法相關的資料

熱點內容
查看伺服器外網訪問地址 瀏覽:854
魔獸爭霸地圖最新加密 瀏覽:682
暢捷雲APP怎麼l發票 瀏覽:209
黑馬程序員與傳智播客 瀏覽:517
geany不能編譯中文嗎 瀏覽:521
和平精英怎麼開啟新伺服器 瀏覽:539
單片機的典型應用 瀏覽:376
vivo手機怎麼對qq進行加密 瀏覽:609
gcc編譯器的鏈接腳本 瀏覽:576
伺服器p01是什麼 瀏覽:909
程序員當保鏢視頻 瀏覽:343
有用友加密狗怎麼下載對應的版本 瀏覽:384
高級語言程序必須經過編譯嗎 瀏覽:53
ce54重新編譯 瀏覽:879
蘋果x手機的app如何加密 瀏覽:475
伺服器如何安裝麒麟 瀏覽:856
單片機控制p1口 瀏覽:701
python子線程通知主線程 瀏覽:923
xp系統網卡驅動哪個文件夾 瀏覽:166
電信網路中心伺服器地址是什麼 瀏覽:109