Bonjour,
Ce topic fait suite à mon précédent : http://www.developpez.net/forums/d12...ganiser-liste/
J'ai résolu mon problème, seulement j'ai du faire une succession de for imbriqués les uns dans les autres...ce qui est très sale, avez vous une idée de comment simplifier ce morceau de code, où le rendre récursif, l'idée serait de pouvoir appeler autant de for que je le souhaite...
La succession de boucles en question:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 for (int i=0; i<firstIndentList.size(); i++){ if (compareList() == true) break; treeList.add(firstIndentList.get(i)); for (int j=0; j<linkTableList.size(); j++){ if (linkTableList.get(j) == resultList.get(i)){ treeList.add(firstIndentList.get(j+1)); for (int k=0; k<linkTableList.size(); k++){ if (linkTableList.get(k) == resultList.get(j+1)){ treeList.add(firstIndentList.get(k+1)); for (int l=0; l<linkTableList.size(); l++){ if (linkTableList.get(l) == resultList.get(k+1)){ treeList.add(firstIndentList.get(l+1)); for (int m=0; m<linkTableList.size(); m++){ if (linkTableList.get(m) == resultList.get(l+1)){ treeList.add(firstIndentList.get(m+1)); } } } } } } } } }
Et mon code qui compile:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 package arbre; import java.util.ArrayList; import java.util.List; public class Arbre { public static List<String> firstIndentList = new ArrayList<String>(); public static List<String> resultList = new ArrayList<String>(); public static List<String> linkTableList = new ArrayList<String>(); public static List<String> treeList = new ArrayList<String>(); public static void main(String[] args) throws Throwable{ createTestTable(); for (int i=0; i<firstIndentList.size(); i++){ if (compareList() == true) break; treeList.add(firstIndentList.get(i)); for (int j=0; j<linkTableList.size(); j++){ if (linkTableList.get(j) == resultList.get(i)){ treeList.add(firstIndentList.get(j+1)); for (int k=0; k<linkTableList.size(); k++){ if (linkTableList.get(k) == resultList.get(j+1)){ treeList.add(firstIndentList.get(k+1)); for (int l=0; l<linkTableList.size(); l++){ if (linkTableList.get(l) == resultList.get(k+1)){ treeList.add(firstIndentList.get(l+1)); for (int m=0; m<linkTableList.size(); m++){ if (linkTableList.get(m) == resultList.get(l+1)){ treeList.add(firstIndentList.get(m+1)); } } } } } } } } } for(int z=0; z<treeList.size(); z++){ System.out.println(treeList.get(z)); } } public static Boolean compareList(){ boolean fin=false; if (resultList.size() == treeList.size()) fin=true; return fin; } public static void createTestTable(){ resultList.add("CX_MVT"); resultList.add("S_PROD_INT"); resultList.add("S_PROD_INT_X"); resultList.add("CX_OPEIND"); resultList.add("S_ASSET"); resultList.add("S_BU"); resultList.add("S_ORG_BU"); resultList.add("S_ORG_EXT"); resultList.add("S_ORG_EXT_X"); resultList.add("S_ORG_INT"); resultList.add("S_PARTY"); resultList.add("S_PARTY_RPT_REL"); resultList.add("S_SRV_REQ"); resultList.add("S_SRV_REQ3_FNX"); resultList.add("S_CONTACT"); resultList.add("S_USER"); resultList.add("S_ORG_EXT_FNX"); resultList.add("S_CP_EMP_PER"); resultList.add("S_EMP_PER"); resultList.add("S_PARTY_PER"); firstIndentList.add("CX_MVT"); firstIndentList.add(" S_PROD_INT"); firstIndentList.add(" S_PROD_INT_X"); firstIndentList.add(" CX_OPEIND"); firstIndentList.add(" S_ASSET"); firstIndentList.add(" S_BU"); firstIndentList.add(" S_ORG_BU"); firstIndentList.add(" S_ORG_EXT"); firstIndentList.add(" S_ORG_EXT_X"); firstIndentList.add(" S_ORG_INT"); firstIndentList.add(" S_PARTY"); firstIndentList.add(" S_PARTY_RPT_REL"); firstIndentList.add(" S_SRV_REQ"); firstIndentList.add(" S_SRV_REQ3_FNX"); firstIndentList.add(" S_CONTACT"); firstIndentList.add(" S_USER"); firstIndentList.add(" S_ORG_EXT_FNX"); firstIndentList.add(" S_CP_EMP_PER"); firstIndentList.add(" S_EMP_PER"); firstIndentList.add(" S_PARTY_PER"); linkTableList.add("CX_MVT"); linkTableList.add("CX_MVT"); linkTableList.add("CX_MVT"); linkTableList.add("CX_MVT"); linkTableList.add("S_PROD_INT"); linkTableList.add("S_PROD_INT"); linkTableList.add("S_PROD_INT"); linkTableList.add("S_PROD_INT"); linkTableList.add("S_PROD_INT"); linkTableList.add("S_PROD_INT"); linkTableList.add("S_PROD_INT"); linkTableList.add("CX_OPEIND"); linkTableList.add("CX_OPEIND"); linkTableList.add("CX_OPEIND"); linkTableList.add("CX_OPEIND"); linkTableList.add("S_ASSET"); linkTableList.add("S_SRV_REQ"); linkTableList.add("S_SRV_REQ"); linkTableList.add("S_CONTACT"); } }
Partager