Bonjour,
Je souhaite savoir comment liberer les resources en java, c'est a dire l'equivalent du gabge collector en c++.
Le code ci dessous fonctionne sans probleme pour des tableaux de petite taille, et j ai cette erreur des lors que j'augmente la taille du tableau
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
java.lang.StringIndexOutOfBoundsException: String index out of range: -50904
	at java.lang.String.charAt(Unknown Source)String index out of range: -50905
String index out of range: -50906 at chaine.main(chaine.java:56)
Voici le code et la ligne 56 est en rouge.
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
import java.util.ArrayList;
// lister les chaine ceci est la derniere version 
public class chaine {
	// // TODO Auto-generated method stub
	
	static String P[]= {"00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", 
		"01010", "01011", "01100", "01101"," 01110"," 01111"," 10000", "10001", "10010", "10011",
		"10100", "10101", "10110", "10111", "11000", "11001", "11010", "11011", "11100", "11101", "11110", "11111"};
	

	public static void main(String[] args) {

		int nbreElt = P.length;
		System.out.println("Taille du tableau= " + nbreElt);
		ArrayList<String> tableau = new ArrayList<String>(nbreElt);
		ArrayList<String> x = new ArrayList<String>(1);
		ArrayList<String> chaine = new ArrayList<String>();

		char charElt = 'a';
		char char_x = 'a';
		for (int i = 0; i < P.length; i++) {
			tableau.add(P[i]);
		}

		String ch = tableau.get(0);

		int cTab = 1;
		System.out.print("Tableau[" + cTab + "] = ");
		System.out.print(tableau);

		while (!tableau.isEmpty()) {

			x.removeAll(x);
			ch = tableau.get(0);
			x.add(0, ch);

			chaine.add(tableau.get(0));

			String elt_x = x.get(0);
			int pos = 0;
			System.out.println("taille tableau"+ tableau.size());
			for (int i = 1; i < tableau.size(); i++) {

				String elt = tableau.get(i);
				int taille_x = elt_x.length();
				int tailleElt = elt.length();
				int k = 1;
				int tail = tailleElt;

				char_x = elt_x.charAt(--taille_x);
				charElt = elt.charAt(--tailleElt);

				while (tailleElt != 0) {
					try {
						if (char_x <= charElt) {
				//erreur ici	char_x = elt_x.charAt(--taille_x);
							charElt = elt.charAt(--tailleElt);
							k++;
							System.out.println("k="+k);
						} else {

							break;
						}

					} catch (IndexOutOfBoundsException e) {
						System.out.println(e.getMessage());
						e.printStackTrace();
					}
				}
				if (k == tail) {
					chaine.add(elt);
					x.removeAll(x);
					pos = i;
					ch = tableau.get(pos);
					x.add(0, ch);
					elt_x = x.get(0);

				} else {
					x.removeAll(x);
					ch = tableau.get(pos);
					x.add(0, ch);
					elt_x = x.get(0);
				}

			}
			System.out.println();
			
			System.out.print("Chaine[" + cTab + "]= ");
			System.out.print(chaine);
									
			System.out.println();
			for (int i = 0; i < tableau.size(); i++) {
				for (int j = 0; j < chaine.size(); j++) {
					if (tableau.get(i).equals(chaine.get(j))) {
						tableau.remove(i);
						

					}
				}
			}
			
			cTab++;
			System.out.println(" ");
			System.out.print("Tableau[" + cTab + "] = ");
			System.out.print(tableau);
			String[] tab = new String[tableau.size()];

			for (int i = 0; i < tableau.size(); i++) {
				tab[i] = tableau.get(i);
			}
			tableau.removeAll(tableau);
			chaine.removeAll(chaine);
			x.removeAll(x);

			for (int i = 0; i < tab.length; i++) {
				tableau.add(tab[i]);
			}

		}

	}
}