Bonjour,
j'ai un souci avec l'utilisation des Hashtable et des ArrayList :
Tout se passe bien dans une boucle, je vérifie les résultats à l'aide de System?out.println(), et lorsque je sors de la boucle, ces affichages ne correspondent plus...
Plutôt que d'expliquer mon problème avec un long bloc, je vous propose mon code commenté.
PS: j'ai tout placé dans une petite classe main afin d'identifier le problème
Ca fait un petit moment que je rame dessus, et j'ai eu beau multiplier les tests, je ne trouve pas mon erreur...
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 static Properties prop = new Properties(); private static void chargerProp(String nomFichier) { try { FileInputStream in = new FileInputStream(nomFichier); prop.load(in); in.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String[] nomFichierSansExtension; String valeur; ArrayList<Fichier> al = new ArrayList<Fichier>(); String[] tab = { "toto.cfg", "titi.cfg" }; Hashtable<String, String> ht = new Hashtable<String, String>(); String elements[]; for (int i = 0; i < tab.length; i++) { // on obtient toto par exemple nomFichierSansExtension = tab[i].split("\\."); // lecture des éléments à chercher dans un fichier chargerProp("donnees.properties"); elements = prop.getProperty(nomFichierSansExtension[0]).split(";"); chargerProp(tab[i]); ht.clear(); for (String str : elements) { valeur = prop.getProperty(str); ht.put(str, valeur); } //ici, hashtable donne ce que j'attend System.out.println(ht.toString()); al.add(new Fichier(tab[i], ht)); //et l'arraylist aussi System.out.println(al.get(i).toString()); } //la ,j'obtiens toujours la dernière valeur du hashtable, alors que le nom change bien. for (int i = 0; i < al.size(); i++) { System.out.println(al.get(i).getNom()); System.out.println(al.get(i).getDonnees().toString()); } }
Arrivez-vous à voir où se situe mon problème?
Merci![]()
Partager