IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Simplifier boucles for imbriquées (récursivité?)


Sujet :

Java

  1. #1
    Membre habitué Avatar de mailbox
    Profil pro
    Inscrit en
    Février 2010
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Février 2010
    Messages : 140
    Points : 159
    Points
    159
    Par défaut Simplifier boucles for imbriquées (récursivité?)
    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");
    	}
    }

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Je ne comprend pas bien ce que tu veux mais la comparaison entre des objets de fait par la méthode equals mais pas avec l'opérateur "==".

    A+.

  3. #3
    Membre éprouvé Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Points : 1 050
    Points
    1 050
    Par défaut
    à supposer que tes 2 listes sont toujours de cette forme:
    - resultList qui liste les tables, une table parente étant listée avant une table enfant
    - linkTableList qui liste la table parent de chaque table, en mettant un null au début pour la table root => les 2 listes ont la même taille

    alors tu peux utiliser cette simple représentation d'arbre:
    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
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
     
    public class TestTri {
    	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 void main(String[] args) throws Throwable{
    		createTestTable();
     
    		HashMap<String, Node> allNodes = new HashMap<String, Node>();
    		Node root = null;
     
    		// Tree initialisation
    		for (int i = 0; i < linkTableList.size(); i++) {
    			String table = resultList.get(i);
    			String dependDe = linkTableList.get(i);
     
    			Node parent = allNodes.get(dependDe);
    			Node child = new Node(table, parent);
     
    			allNodes.put(table, child);
    			if (parent != null) {
    				parent.addChild(allNodes.get(table));
    			}else {
    				root = child;
    			}
    		}
     
    		// Tree display
    		displayTree(root, "");
    	}
     
    	private static void displayTree(Node node, String prefix) {
    		System.out.println(prefix+node.getName());
    		for (Node child : node.getChildren()) {
    			displayTree(child, "\t"+prefix);
    		}
    	}
     
    	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");
     
    		linkTableList.add(null);
    		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");
    	}
    }
    class Node{
    	private String name;
    	private Node parent = null;
    	private ArrayList<Node> children = new ArrayList<Node>();
    	public Node(String name) {
    		this.name = name;
    	}
    	public Node(String name, Node parent) {
    		this.name = name;
    		this.parent = parent;
    	}
    	public void addChild(Node node) {
    		this.children.add(node);
    	}
    	public String getName() {
    		return this.name;
    	}
    	public ArrayList<Node> getChildren() {
    		return this.children;
    	}
    	public Node getParent() {
    		return this.parent;
    	}
    }
    ce genre de solution te va-t'elle ?
    Nous les geeks, c'est pas qu'on a une case en moins, c'est juste qu'on compte à partir de zéro.
    Plus les choses changent, plus elles restent les mêmes

  4. #4
    Membre habitué Avatar de mailbox
    Profil pro
    Inscrit en
    Février 2010
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Février 2010
    Messages : 140
    Points : 159
    Points
    159
    Par défaut
    En tout ça fonctionne Mais je n'avais encore jamais fait de liste chainée, je suis entrain de regarder pour comprendre comment fonctionne le code^^

    Par exemple je vois que tu n'utilises plus la liste firstIndentList

  5. #5
    Membre éprouvé Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Points : 1 050
    Points
    1 050
    Par défaut
    ce n'est pas vraiment une liste chaînée, c'est la création d'un arbre et son parcours récursif.

    la boucle for crée l'arbre en attribuant à chaque table (ou Node) son parent (et inversement, l'enfant à son parent), à part pour la table la plus haute qui n'a pas de parent.

    la fonction displayTree affiche récursivement l'arbre, l'indentation est donnée par la profondeur de récursion (qui est égale au nombre de parents que possède l'enfant), du coup oui plus besoin de la 3e liste.
    Nous les geeks, c'est pas qu'on a une case en moins, c'est juste qu'on compte à partir de zéro.
    Plus les choses changent, plus elles restent les mêmes

  6. #6
    Membre habitué Avatar de mailbox
    Profil pro
    Inscrit en
    Février 2010
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Février 2010
    Messages : 140
    Points : 159
    Points
    159
    Par défaut
    J'ai fait plusieurs tests avec ton code et c'est parfait

    J'aurai juste une question, si je veux afficher dans l'arbre à côté de chaque table sa clé primaire (je les ai dans une liste au même format que la liste des tables), je dois tout repenser où tu sais comment l'ajouter simplement?

    Je le faisais dans mon code auparavant mais je ne l'avais pas mis dans le topic pour ne pas surcharger (ça ne m'est pas indispensable mais sait-on jamais).

    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
    		pKeyList.add("M-184222436");
    		pKeyList.add("1-LSC6");
    		pKeyList.add("1-LSC6");
    		pKeyList.add("O-16942789");
    		pKeyList.add("0-1-6KJ8-972");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("0-R9NH");
    		pKeyList.add("1-20KTZ9");
    		pKeyList.add("1-20KTZ9");
    		pKeyList.add("1-6K09-2988");
    		pKeyList.add("1-6K09-2988");
    		pKeyList.add("1-6AFR-27");
    		pKeyList.add("1-6363R");
    		pKeyList.add("1-6363R");
    		pKeyList.add("1-6K0A-2997");

  7. #7
    Membre éprouvé Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Points : 1 050
    Points
    1 050
    Par défaut
    puisque la liste de clés aura la même taille que la liste que tu utilises pour construire l'arbre, il te suffit d'ajouter une propriété primaryKey avec un getter/setter à la classe Node. Tu l'initialises quand tu crée l'arbre, et tu l'affiches dans displayTree
    Nous les geeks, c'est pas qu'on a une case en moins, c'est juste qu'on compte à partir de zéro.
    Plus les choses changent, plus elles restent les mêmes

  8. #8
    Membre habitué Avatar de mailbox
    Profil pro
    Inscrit en
    Février 2010
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Février 2010
    Messages : 140
    Points : 159
    Points
    159
    Par défaut
    Merci c'est parfait

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Boucles for imbriquées
    Par The eye dans le forum ASP
    Réponses: 2
    Dernier message: 19/07/2007, 12h00
  2. Boucle for imbriqué
    Par boula dans le forum Shell et commandes GNU
    Réponses: 2
    Dernier message: 18/07/2007, 12h42
  3. 2 boucles for imbriquées
    Par karimphp dans le forum Langage
    Réponses: 8
    Dernier message: 02/12/2006, 14h46
  4. Batch - Deux boucle For imbriquées plus un FC
    Par Lorponos dans le forum Windows
    Réponses: 17
    Dernier message: 27/07/2006, 14h58
  5. [Syntaxe] Boucle For imbriquées en 1.5
    Par Piolet dans le forum Langage
    Réponses: 5
    Dernier message: 09/01/2005, 00h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo