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
| // appeler le fichier Test.java
// par Nicolas_75
// lundi 19 février 2007
// pour http://www.developpez.net/forums/showthread.php?t=282001
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
int[] values = {2, 21, 36, 11, 2, 54, 36, 2, 58, 6};
// création d'une ArrayList sans doublon :
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i=0; i<values.length; i++) {
if (!temp.contains(new Integer(values[i]))) {
temp.add(new Integer(values[i]));
}
}
// création du nouveau tableau :
int[] result = new int[temp.size()];
for (int i=0; i<temp.size(); i++) {
result[i] = temp.get(i);
}
// affichage du résultat :
System.out.println("Résultat :");
if (result.length>0) {
System.out.print(result[0]);
if (result.length>1) {
for (int i=1; i<result.length; i++) {
System.out.print(" "+result[i]);
}
}
}
System.out.println();
}
} |
Partager