-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRI Java 6.txt
More file actions
4801 lines (4342 loc) · 119 KB
/
RI Java 6.txt
File metadata and controls
4801 lines (4342 loc) · 119 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************************/
/********************************** Chapitre 1 ****************************************/
/**************************************************************************************/
/********* Passage d'arguments à une application Java au moment de l'exécution ********/
/* Déclaration de la classe principale de l'application */
public class MaClasse
{
/* Déclaration de la méthode point d'entrée de l'application*/
public static void main(String args[])
{
/* Affichage des arguments de la ligne de commande */
for (int i = 0 ; i < args.length; i++)
{
System.out.printIn("Argument " +i + " = " + args[i]) ;
}
/* Conversion de deux arguments de la ligne de commande de String vers int, puis addition des valeurs entières, et affichage du résultat */
int somme;
somme=(Integer.parselnt(args[3]))+(Integer.parselnt(args[4]));
System.out.println("Argument 3 + Argument 4 = " + somme);
}
}
/**************************************************************************************/
/********************************** Chapitre 2 ****************************************/
/**************************************************************************************/
/********* concaténation de chaînes de caractères ********/
public class TestString
{
public static void main(String[] args)
{
long duree;
String lievre;
String tortue="";
long debut, fin;
debut = System.currentTimeMillis();
for (int i = 0; i <= 10000; i++)
{
tortue = tortue + " " + i;
}
fin = System.currentTimeMillis();
duree = fin-debut;
System.out.println("durée pour la tortue : " + duree + "ms");
debut = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
for (int i = 0; i <= 10000; i++)
{
sb.append(" ");
sb.append(i);
}
lievre = sb.toString();
fin = System.currentTimeMillis();
duree = fin-debut;
System.out.println("durée pour le lievre : " + duree + "ms");
if (lievre.equals(tortue))
{
System.out.println("les deux chaines sont identiques");
}
}
}
/********* structure de boucle ********/
import java.io.IOException;
public class TestStructures
{
static boolean stop;
public static void main(String[] args)
{
new Thread()
{
public void run()
{
int c;
try
{
c=System.in.read();
stop=true;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
long compteur=0;
while(true)
{
compteur++;
if (compteur%2==0)
continue;
if (stop)
break;
System.out.println(compteur);
}
}
}
/**************************************************************************************/
/********************************** Chapitre 3 ****************************************/
/**************************************************************************************/
/********* Classe Personne avec attributs et méthodes ********/
public class Personne
{
private String nom;
private String prenom;
private GregorianCalendar date_nais;
public long calculAge()
{
long age;
date_nais=new GregorianCalendar(1963,11,29);
age=new GregorianCalendar().getTimeInMillis()-date_nais.getTimeInMillis();
age=age/1000/60/60/24/365;
return age;
}
public void affichage()
{
System.out.println("nom : " + nom);
System.out.println("prenom : " + prenom);
System.out.println("age : " + calculAge());
}
}
/************** fonction affichage surchargée **************/
public void affichage(boolean francais)
{
if (francais)
{
System.out.println("nom : " + nom);
System.out.println("prenom : " + prenom);
System.out.println("age : " + calculAge());
}
else
{
System.out.println("name : " + nom);
System.out.println("first name : " + prenom);
System.out.println("age : " + calculAge());
}
}
/************** deuxième fonction affichage surchargée illégale **************/
public void affichage(boolean majuscule)
{
if (majuscule)
{
System.out.println("nom : " + nom.toUpperCase());
System.out.println("prenom : " + prenom.toUpperCase());
System.out.println("age : " + calculAge());
}
else
{
System.out.println("nom : " + nom.toLowerCase());
System.out.println("prenom : " + prenom.toLowerCase());
System.out.println("age : " + calculAge());
}
}
/*********** fonction avec un nombre quelconque de paramètres **************/
public void affichage(String...couleurs)
{
if (couleurs==null)
{
System.out.println("pas de couleur");
return;
}
switch (couleurs.length)
{
case 1:
System.out.println("une couleur");
break;
case 2:
System.out.println("deux couleurs");
break;
case 3:
System.out.println("trois couleurs");
break;
default :
System.out.println("plus de trois couleurs");
}
}
/************** les accesseurs ***************/
public class Personne
{
private String nom;
private String prenom;
private GregorianCalendar date_nais;
public String getNom()
{
return nom;
}
public void setNom(String n)
{
nom = n.toUpperCase();
}
public String getPrenom()
{
return prenom;
}
public void setPrenom(String p)
{
prenom = p.toLowerCase();
}
}
/************* les accesseurs avec règles de gestion ************/
public class Personne
{
private String nom;
private String prenom;
private GregorianCalendar date_nais;
// champ privé représentant le numéro de la Personne
private int numéro;
// champ statique privé représentant le compteur de Personnes
private static int nbInstances;
public String getNom()
{
return nom;
}
public void setNom(String n)
{
nom = n.toUpperCase();
}
public String getPrenom()
{
return prenom;
}
public void setPrenom(String p)
{
prenom = p.toLowerCase();
}
// méthode d’instance permettant d’obtenir le numéro d’une Personne
public int getNuméro()
{
return numéro;
}
// methode statique permettant d’obtenir le nombre d’instances créées
public static int getNbInstances()
{
return nbInstances;
}
public Personne()
{
nom="";
prenom="";
date_nais=null;
// création d’une nouvelle Personne donc incrémentation du compteur
nbInstances++;
// affectation à la nouvelle Personne de son numéro
numéro=nbInstances;
}
}
/************** champs et méthodes statiques **************/
import java.util.GregorianCalendar;
public class Personne
{
private String nom="nouveauNom";
private String prenom="nouveauPrenom";
private GregorianCalendar date_nais=new GregorianCalendar(1900,01,01);
private int numéro=0;
private static int numInstance;
public String getNom()
{
return nom;
}
public void setNom(String n)
{
nom = n.toUpperCase();
}
public String getPrenom()
{
return prenom;
}
public void setPrenom(String p)
{
prenom = p.toLowerCase();
}
@Override
protected void finalize() throws Throwable
{
System.out.print("\u2020");
super.finalize();
}
public int getNuméro()
{
return numéro;
}
public static int getNbInstances()
{
return numInstance;
}
public Personne()
{
nom="";
prenom="";
date_nais=null;
numInstance++;
numéro=numInstance;
}
public Personne(String n,String p,GregorianCalendar d)
{
nom=n;
prenom=p;
date_nais=d;
numInstance++;
numéro=numInstance;
}
}
/************ action du Garbage Collector ******************/
import java.util.GregorianCalendar;
public class Personne
{
private String nom="nouveauNom";
private String prenom="nouveauPrenom";
private GregorianCalendar date_nais=new GregorianCalendar(1900,01,01);
private int numéro=0;
private static int numInstance;
public String getNom()
{
return nom;
}
public void setNom(String n)
{
nom = n.toUpperCase();
}
public String getPrenom()
{
return prenom;
}
public void setPrenom(String p)
{
prenom = p.toLowerCase();
}
@Override
protected void finalize() throws Throwable
{
System.out.print("\u2020");
super.finalize();
}
public int getNuméro()
{
return numéro;
}
public static int getNbInstances()
{
return numInstance;
}
public Personne()
{
nom="";
prenom="";
date_nais=null;
numInstance++;
numéro=numInstance;
}
public Personne(String n,String p,GregorianCalendar d)
{
nom=n;
prenom=p;
date_nais=d;
numInstance++;
numéro=numInstance;
}
}
import java.util.GregorianCalendar;
public class GestionMemoire
{
public static void main(String[] args) throws InterruptedException
{
double total;
double reste;
double pourcentage;
for (int j=0;j<1000;j++)
{
creationTableau();
total=Runtime.getRuntime().totalMemory();
reste=Runtime.getRuntime().freeMemory();
pourcentage=100-(reste/total)*100;
System.out.println("creation du " + j + "ieme tableau memoire pleine a : " + pourcentage + "%" );
// une petite pause pour pouvoir lire les messages
Thread.sleep(1000);
}
}
public static void creationTableau()
{
// creation d’un tableau de 1000 Personnes dans une variable locale
// a la fin de cette fonction les elements du tableau ne sont plus
// accessibles et peuvent être supprimes de la memoire
Personne[] tablo;
tablo=new Personne[1000];
for (int i=0;i<1000;i++)
{
tablo[i]=new Personne("Dupont","albert",new GregorianCalendar(1956,12,13));
}
}
}
/************ sans utilisation de this et super *****************/
public void affichage()
{
System.out.println("nom : " + getNom());
System.out.println("prenom : " + getPrenom());
System.out.println("age : " + calculAge());
switch (type)
{
case 'P':
System.out.println("type de client : Particulier");
break;
case 'E':
System.out.println("type de client : Entreprise");
break;
case 'A':
System.out.println("type de client : Administration");
break;
default:
System.out.println("type de client : Inconnu");
break;
}
}
/************ avec utilisation de this et super ***************/
public void affichage()
{
super.affichage();
switch (type)
{
case 'P':
System.out.println("type de client : Particulier");
break;
case 'E':
System.out.println("type de client : Entreprise");
break;
case 'A':
System.out.println("type de client : Administration");
break;
default:
System.out.println("type de client : Inconnu");
break;
}
}
/************* classe abstraite *****************/
public abstract class EtreVivant
{
private double taille;
private double poids;
public double getTaille()
{
return taille;
}
public void setTaille(double taille)
{
this.taille = taille;
}
public double getPoids()
{
return poids;
}
public void setPoids(double poids)
{
this.poids = poids;
}
// cette méthode devra etre implémentée dans les classes dérivées
protected abstract void seDeplacer();
}
/************ Clonage ***************/
public class Commande implements Cloneable
{
Client leClient;
LignesDeCommande lesLignes;
public Commande()
{
super();
lesLignes=new LignesDeCommande();
}
public Object clone() throws CloneNotSupportedException
{
Commande cmd;
// création d'une copie de la commande
cmd=(Commande)super.clone();
// duplication des lignes de la commande
cmd.lesLignes=(LignesDeCommande)lesLignes.clone();
return cmd;
}
public Client getLeClient()
{
return leClient;
}
public void setLeClient(Client leClient)
{
this.leClient = leClient;
}
public LignesDeCommande getLesLignes()
{
return lesLignes;
}
public void setLesLignes(LignesDeCommande lesLignes)
{
this.lesLignes = lesLignes;
}
}
public class LignesDeCommande implements Cloneable
{
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
Client c;
c=new Client("ENI","",new GregorianCalendar(1981,05,15),'E');
Commande cmd1,cmd2;
// création et initialisation d'une commande
cmd1=new Commande();
cmd1.setLeClient(c);
System.out.println("hashCode de la commande : " +cmd1.hashCode());
System.out.println("hashCode du Client : " +cmd1.getLeClient().hashCode());
System.out.println("hashCode des lignes : " +cmd1.getLesLignes().hashCode());
cmd2=(Commande)cmd1.clone();
System.out.println("hashCode de la copie : " +cmd2.hashCode());
System.out.println("hashCode du Client de la copie: " +cmd2.getLeClient().hashCode());
System.out.println("hashCode des lignes de la copie:"+cmd2.getLesLignes().hashCode());
/************** La méthode equals *****************/
public boolean equals(Object obj)
{
Client c;
// verification si obj est null ou reférence une instance
// d'une autre classe
if (obj ==null || obj.getClass()!=this.getClass())
{
return false;
}
else
{
c=(Client)obj;
// verification des critères d'égalité sur
// - le nom
// - le prenom
// - la date de naissance
// - le type de client
if (c.getNom().equals(getNom())&
c.getPrenom().equals(getPrenom()) &
c.getDate_nais().equals(getDate_nais()) &
c.getType()== getType() )
{
return true;
}
else
{
return false;
}
}
}
/************** getClass (informations sur la classe) ***************/
public static void infoClasse(Object o)
{
Class c;
c=o.getClass();
System.out.println("nom de la classe : " + c.getName());
System.out.println("elle est dans le package : " + c.getPackage().getName());
System.out.println("elle herite de la classe : " + c.getSuperclass().getName());
System.out.println("elle possède les champs : ");
for (int i=0;i<c.getFields().length;i++)
{
System.out.print("\t" + c.getFields()[i].getName());
System.out.println(" de type :" + c.getFields()[i].getType().getName());
}
System.out.println("elle possède les méthodes : ");
for (int i=0;i<c.getMethods().length;i++)
{
System.out.print("\t" + c.getMethods()[i].getName());
System.out.print(" qui attend comme paramètre (");
for (int j=0;j<c.getMethods()[i].getParameterTypes().length;j++)
{
System.out.print(c.getMethods()[i].getParameterTypes()[j]+ " ");
}
System.out.println(")");
}
}
/************* création d'une interface **************/
// cette interface devra être implémentée par les classes
// pour lesquelles un classement des instances est envisagé
public interface Classable
{
// cette méthode pourra être appelée pour comparer l'instance courante
// avec celle reçue en paramètre
// la méthode retourne un entier dont la valeur dépend des règles suivantes
// 1 si l'instance courante est supérieure à celle reçue en paramètre
// 0 si les deux instances sont égales
// -1 si l'instance courante est inférieure à celle reçue en paramètre
// -99 si la comparaison est impossible
int compare(Object o);
public static final int INFERIEUR=-1;
public static final int EGAL=0;
public static final int SUPERIEUR=1;
public static final int ERREUR=-99;
}
/************* implémentation d'une interface **************/
public class Personne
implements Classable
{
public int compare(Object o)
{
Personne p;
if (o instanceof Personne)
{
p=(Personne)o;
}
else
{
return Classable.ERREUR;
}
if (getNom().compareTo(p.getNom())<0)
{
return Classable.INFERIEUR;
}
if (getNom().compareTo(p.getNom())>0)
{
return Classable.SUPERIEUR;
}
return Classable.EGAL;
}
}
/************ Utilisation d'une interface *************/
public static Classable[] tri(Classable[] tablo)
{
int i,j;
Classable c;
for (i=0;i< tablo.length;i++)
{
for( j = i + 1; j<tablo.length;j++)
{
if (tablo[j].compare(tablo[i])==Classable.INFERIEUR)
{
c = tablo[j];
tablo[j] = tablo[i];
tablo[i] = c;
}
else if (tablo[j].compare(tablo[i])==Classable.ERREUR)
{
return null;
}
}
}
return tablo;
}
/************** test de l'interface ***************/
Personne[] tab;
tab=new Personne[5];
tab[0] = new Personne("toto2", "prenom2",new GregorianCalendar(1922,2,15));
tab[1] = new Personne("toto1", "prenom1 ",new GregorianCalendar(1911,1,15));
tab[2] = new Personne("toto5", "prenom5 ",new GregorianCalendar(1955,05,15));
tab[3] = new Personne("toto3", "prenom3 ",new GregorianCalendar(1933,03,15));
tab[4] = new Personne("toto4", "prenom4 ",new GregorianCalendar(1944,04,15));
Personne[] tabTrie;
tabTrie=(Personne[])tri(tab);
for (int i=0;i<tabTrie.length;i++)
{
System.out.println(tabTrie[i]);
}
/*************** classes anonymes *****************/
public static Object[] tri(Object[] tablo,Comparateur trieur)
{
int i,j;
Object c;
Object[] tabloTri;
tabloTri=Arrays.copyOf(tablo,tablo.length);
for (i=0;i< tabloTri.length;i++)
{
for( j = i + 1; j<tabloTri.length;j++)
{
// utilise la fonction compare de l’objet reçu en parameter pour
// comparer le contenu de deux cases du tableau
if (trieur.compare(tabloTri[j],tabloTri[i])==Comparateur.INFERIEUR)
{
c = tabloTri[j];
tabloTri[j] = tabloTri[i];
tabloTri[i] = c;
}
else if (trieur.compare(tabloTri[j],tabloTri[i])==Comparateur.ERREUR)
{
return null;
}
}
}
return tabloTri;
}
Personne[] tab;
tab=new Personne[5];
tab[0] = new Personne("toto2", "prenom2",new GregorianCalendar(1922,2,15));
tab[1] = new Personne("toto1", "prenom1 ",new GregorianCalendar(1911,1,15));
tab[2] = new Personne("toto5", "prenom5 ",new GregorianCalendar(1955,05,15));
tab[3] = new Personne("toto3", "prenom3 ",new GregorianCalendar(1933,03,15));
tab[4] = new Personne("toto4", "prenom4 ",new GregorianCalendar(1944,04,15));
tabTrie=(Personne[])tri(tab,
// creation d'une instance de classe implementant l'interface Comparateur
new Comparateur()
// voici le code de la classe
{
// comme l'exige l'interface voici la méthode compare
public int compare(Object o1, Object o2)
{
Personne p1,p2;
if (o1 instanceof Personne & o2 instanceof Personne)
{
p1=(Personne)o1;
p2=(Personne)o2;
}
else
{
return Classable.ERREUR;
}
if (p1.getNom().compareTo(p2.getNom())<0)
{
return Classable.INFERIEUR;
}
if (p1.getNom().compareTo(p2.getNom())>0)
{
return Classable.SUPERIEUR;
}
return Classable.EGAL;
} // parenthese de fermeture de la methode compare
} // parenthese de fermeture de la classe
); // fin de l'appel de la fonction de tri
// affichage du tableau trié
for (int i=0;i<tabTrie.length;i++)
{
System.out.println(tabTrie[i]);
}
/************* classes anonymes deuxième version *************/
tabTrie=(Personne[])tri(tab,
// creation d'une instance de classe implementant l'interface Comparateur
new Comparateur()
// voici le code de la classe
{
// comme l'exige l'interface voici la méthode compare
public int compare(Object o1, Object o2)
{
Personne p1,p2;
if (o1 instanceof Personne & o2 instanceof Personne)
{
p1=(Personne)o1;
p2=(Personne)o2;
}
else
{
return Classable.ERREUR;
}
if (p1.calculAge()<p2.calculAge())
{
return Classable.INFERIEUR;
}
if (p1.calculAge()>p2.calculAge())
{
return Classable.SUPERIEUR;
}
return Classable.EGAL;
} // parenthese de fermeture de la methode
} // parenthese de fermeture de la classe
); // fin de l'appel de la fonction de tri
for (int i=0;i<tabTrie.length;i++)
{
System.out.println(tabTrie[i]);
}
/*************** classe générique ****************/
import java.util.ArrayList;
public class ListeGenerique <T>
{
// pour stocker les éléments de la liste
private ArrayList<T> liste;
// pointeur de position dans la liste
private int position;
//nombre d'élémments de la liste
private int nbElements;
// constructeur avec un parametre permettant de dimensionner la liste
public ListeGenerique(int taille)
{
liste=new ArrayList<T>(taille);
}
public void ajout(T element)
{
liste.add(element);
nbElements = nbElements + 1;
}
public void insert(T element,int index)
{
// on verifie si l'index n'est pas superieur au nombre d'elements
// ou si l'index n'est pas inferieur à 0
if (index >= nbElements || index < 0)
{
return;
}
liste.add(index,element);
// on met a jour le nombre d'elements
nbElements = nbElements + 1;
}
public void remplace(T element,int index)
{
// on verifie si l'index n'est pas superieur au nombre d'elements
// ou si l'index n'est pas inferieur à 0
if (index >= nbElements || index < 0)
{
return;
}
liste.set(index,element);
}
public void supprime(int index)
{
int i;
// on verifie si l'index n'est pas superieur au nombre d'elements
// ou si l'index n'est pas inferieur à 0
if (index >= nbElements || index < 0)
{
return;
}
liste.remove(index);
// on met a jour le nombre d'elements
nbElements = nbElements - 1;
}
public int getTailleListe()
{
return nbElements;
}
public T premier() throws Exception
{
if (nbElements == 0)
{
throw new Exception("liste vide");
}
// on deplace le pointeur sur le premier element
position = 0;
return liste.get(0);
}
public T dernier() throws Exception
{
if (nbElements == 0)
{
throw new Exception("liste vide");
}
// on deplace le pointeur sur le dernier element
position = nbElements - 1;
return liste.get(position);
}
public T suivant() throws Exception
{
if (nbElements == 0)
{
throw new Exception("liste vide");
}
// on verifie si on n'est pas a la fin de la liste
if (position == nbElements - 1)
{
throw new Exception("pas d'element suivant");
}
// on deplace le pointeur sur l'element suivant
position = position + 1;
return liste.get(position);
}
public T precedent() throws Exception
{
if (nbElements == 0)
{
throw new Exception("liste vide");
}
// on verifie si on n'est pas sur le premier element
if (position == 0)
{
throw new Exception("pas d'element precedent");
}
// on se deplace sur l'element precedent
position = position - 1;
return liste.get(position);
}
}
/**************** Utilisation d’une classe générique ******************/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestListeGenerique
{
static ListeGenerique<String> liste = new ListeGenerique<String>(5);
public static void main(String[] args)
{
liste.ajout("premier");
liste.ajout("deuxieme");
liste.ajout("troisieme");
liste.ajout("quatrieme");