/* Fichier Liste0.java * Création et manipulation "technique" d'une liste * (voir l'exécution) */ import java.util.*; public class Liste0 { // créer une liste des diviseurs d'un nombre static LinkedList creerListe(int nombre) { LinkedList liste = new LinkedList(); for(int candi = 1; candi <= nombre; candi++) if(nombre % candi == 0) liste.add(candi); return liste; } static void afficher(LinkedList liste, String mess) { if(mess.length() == 0) System.out.printf("Liste des diviseurs de %d\n", liste.getLast()); else System.out.printf("Liste des diviseurs communs %s:\n", mess); for(int i = 0; i < liste.size(); i++) System.out.printf("%3d) %10d\n", i, liste.get(i)); System.out.printf("\n"); } static LinkedList creerListe(LinkedList divi720, LinkedList divi1000) { LinkedList liste = new LinkedList(); for (int i = 0 ; i < divi720.size(); i++) if (divi1000.contains(divi720.get(i))) liste.add(divi720.get(i)); return liste; } static void demo(LinkedList commune) { System.out.printf("\nDémo de la manipulation de la liste commune:\n"); // quelques ajouts : commune.add(567); commune.addFirst(999); commune.addLast(222); // insertions : commune.add(0, 333); commune.add(4, 666); System.out.printf("commune : %s\n", commune); // modification : commune.set(3, 555); System.out.printf("commune : %s\n", commune); // consultation : // 1er élément System.out.printf("\n - 1er élément par quelques manières :\n"); System.out.printf("%5d\n", commune.get(0)); System.out.printf("%5d\n", commune.getFirst()); System.out.printf("%5d\n", commune.peek()); System.out.printf("%5d\n", commune.peekFirst()); System.out.printf("%5d\n", commune.element()); // dernier élément System.out.printf("\n - dernier élément par quelques manières :\n"); System.out.printf("%5d\n", commune.get(commune.size()-1)); System.out.printf("%5d\n", commune.getLast()); System.out.printf("%5d\n", commune.peekLast()); // à un indice donné : System.out.printf("\n - 5ième élément : %d\n", commune.get(4)); // suppression : commune.remove(); commune.remove(3); commune.remove(new Integer(333)); System.out.printf("commune : %s\n", commune); // recherche séquentielle : int k = commune.indexOf(999); System.out.printf("recherche 999, k vaut : %d\n", k); k = commune.indexOf(777); System.out.printf("recherche 777, k vaut : %d\n", k); k = commune.lastIndexOf(40); System.out.printf("recherche 40, k vaut : %d\n", k); System.out.printf("commune.contains(20) : %s\n", commune.contains(20)); // min, max : System.out.printf(" mini : %d\n", Collections.min(commune)); System.out.printf(" maxi : %d\n", Collections.max(commune)); // tri : Collections.sort(commune); System.out.printf("commune triée : %s\n", commune); // recherche dicho : k = Collections.binarySearch(commune, 222); System.out.printf("recherche 222, k vaut : %d\n", k); k = Collections.binarySearch(commune, 250); System.out.printf("recherche 250, k vaut : %d\n", k); commune.add(-k-1, 250); System.out.printf("commune après inséré 250 : %s\n", commune); } public static void main(String[] args) { final int NOMBRE1 = 720, NOMBRE2 = 1000; LinkedList divi720 = creerListe(NOMBRE1), divi1000= creerListe(NOMBRE2); afficher(divi720,""); afficher(divi1000,""); LinkedList commune = creerListe(divi720, divi1000); afficher(commune,"de 720 et 1000"); demo(commune); // System.out.printf("Bienvenue au Java\n\n"); } } /* Exécution : --------------------Configuration: -------------------- Liste des diviseurs de 720 0) 1 1) 2 2) 3 3) 4 4) 5 5) 6 6) 8 7) 9 8) 10 9) 12 10) 15 11) 16 12) 18 13) 20 14) 24 15) 30 16) 36 17) 40 18) 45 19) 48 20) 60 21) 72 22) 80 23) 90 24) 120 25) 144 26) 180 27) 240 28) 360 29) 720 Liste des diviseurs de 1000 0) 1 1) 2 2) 4 3) 5 4) 8 5) 10 6) 20 7) 25 8) 40 9) 50 10) 100 11) 125 12) 200 13) 250 14) 500 15) 1000 Liste des diviseurs communs de 720 et 1000: 0) 1 1) 2 2) 4 3) 5 4) 8 5) 10 6) 20 7) 40 Démo de la manipulation de la liste commune: commune : [333, 999, 1, 2, 666, 4, 5, 8, 10, 20, 40, 567, 222] commune : [333, 999, 1, 555, 666, 4, 5, 8, 10, 20, 40, 567, 222] - 1er élément par quelques manières : 333 333 333 333 333 - dernier élément par quelques manières : 222 222 222 - 5ième élément : 666 commune : [999, 1, 555, 4, 5, 8, 10, 20, 40, 567, 222] recherche 999, k vaut : 0 recherche 777, k vaut : -1 recherche 40, k vaut : 8 commune.contains(20) : true mini : 1 maxi : 999 commune triée : [1, 4, 5, 8, 10, 20, 40, 222, 555, 567, 999] recherche 222, k vaut : 7 recherche 250, k vaut : -9 commune après inséré 250 : [1, 4, 5, 8, 10, 20, 40, 222, 250, 555, 567, 999] Process completed. */