Bonjour,
j'ai besoin de resortir à partir d'une ArrayList les composantes connexes voici mon code mais malheureusement il ne donne pas la bonne solution. par exemple pour la liste suivante: [24, 36, 35, 27, 14, 35, 23, 18, 12, 27, 27, 31, 12, 15, 28, 11, 23]
il me donne que tous appartiennent a une meme composante connexe alors que c'est pas vrai.
merci d'avance.
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 public ArrayList <Integer> composanteConnexe (ArrayList <Integer> ta){ connexe=false; ListIterator l=ta.listIterator(); //t.add(ta.get(0)); //t.add(ta.get(1)); while(l.hasNext()){ es++; int index=0; Object rech=ta.get(0); ta.remove(0); ListIterator i=ta.listIterator(); while( i.hasNext()){ if((i.next()).equals(rech)){ int x=(i.previousIndex()-1); int x2=(i.previousIndex()+1); if(index % 2!=0 && es % 2!=0){ if(t.contains(rech)||t.contains(ta.get(x2))){ t.add(rech); t.add(ta.get(x2));connexe=true;} } else{ if(index % 2!=0 || !i.hasNext()||es % 2!=0){ if(t.contains(ta.get(x))||t.contains(rech)){ t.add(ta.get(x)); t.add(rech);connexe=true;} } else{ if(t.contains(rech)||t.contains(ta.get(x2))){ t.add(rech); t.add(ta.get(x2));connexe=true;} } } } index++; } } return t; }
Partager