-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTraverser.java
More file actions
90 lines (75 loc) · 2.94 KB
/
Traverser.java
File metadata and controls
90 lines (75 loc) · 2.94 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
class Traverser {
public static void main(String[] args) {
int[] fib = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
String mot = "Généreux";
String[] jours = {
"lundi", "mardi", "mercredi", "jeudi", "vendredi",
"samedi", "dimanche"
};
//
// pour voir juste un seul exemple à la fois, placer les autres derrière
// un commentaire
//
// chaque méthode est définie après main() - elle inclut simplement
// le code fourni dans les notes de cours
//
// traverser avec des boucles while
whileWithNumbers(fib);
sep(); // affiche un séparateur
whileWithText(mot);
// traverser avec des boucles for
sep(); sep();
forWithNumbers(fib);
sep();
forWithText(mot);
// traverser avec des boucles for-each
sep(); sep();
forEachWithTextArray(jours);
sep();
forEachWithNumbers(fib);
sep();
forEachWithText(mot);
}
static void sep(){
System.out.println("\n-------------------------------\n");
}
static void whileWithNumbers( int[] nums ) {
int i = 0; // i représente l'index
while ( i < nums.length ) { // utiliser la taille du tableau comme condition
System.out.println( nums[ i ] ); // ou autre(s) opération(s) avec nums[i]
i++; // changer l'index
}
}
static void whileWithText( String word ) {
int i = 0; // l'index
while ( i < word.length() ) { // utiliser la taille du String comme condition
System.out.println( word.charAt( i ) ); // ou autres opérations sur le char
i++; // changer l'index
}
}
static void forWithNumbers( int[] nums ) {
for ( int i = 0; i < nums.length; i++ ) { // tous les éléments dans la déclaration
System.out.println( nums[ i ] ); // ou autre(s) opération(s) avec nums[i]
}
}
static void forWithText( String word ) {
for( int i = 0; i < word.length(); i++ ) { // tous les éléments dans la déclaration
System.out.println( word.charAt( i ) );
}
}
static void forEachWithTextArray( String[] jours ) {
for ( String jour : jours ) { // se lit "For each String in the String array jours"
System.out.println( jour ); // ou autre opération avec la valeur de l'élément
}
}
static void forEachWithNumbers( int[] nums ) {
for ( int n : nums ) { // se lit "For each int in the int array nums"
System.out.println( n ); // ou autre opération avec la valeur de l'élément
}
}
static void forEachWithText( String word ) {
for ( char letter : word.toCharArray() ) { // se lit "For each char in the char array"
System.out.println( letter ); // ou autre opération avec la valeur de l'élément
}
}
}