Array Traversals · Parcours de tableaux
| English | Français |
|---|---|
| traversal/træˈvɜːsl/ | traversée |
| enhanced for loop/enˈhænst fɔː luːp/ | boucle for améliorée |
Visiting every element
- A traversal 遍历 visits every element of an array, usually in order.
- The standard indexed loop:
for (int i = 0; i < a.length; i++) { ... a[i] ... } isteps from0tolength - 1, soa[i]reaches each element once.- Traversal is the foundation of every array algorithm.
Visiter chaque élément
- Une traversée 遍历 parcourt tous les éléments d'un tableau, généralement dans l'ordre.
- La boucle indexée standard :
for (int i = 0; i < a.length; i++) { ... a[i] ... } iprogresse de0àlength - 1, donca[i]atteint chaque élément une seule fois.- La traversée est la base de tout algorithme sur tableau.
The for-each loop
- The enhanced for loop (for-each) 增强for循环 visits each value without an index:
for (int x : a) { ... x ... }—xtakes each element's value in turn.- Cleaner when you only need the values, not their positions.
- But it can't change the array or use the index.
La boucle for-each
- La boucle for améliorée (for-each) 增强for循环 parcourt chaque valeur sans indice :
for (int x : a) { ... x ... }—xprend successivement la valeur de chaque élément.- Plus propre lorsque vous n'avez besoin que des valeurs, pas de leurs positions.
- Mais elle ne peut pas modifier le tableau ni utiliser l'indice.
Indexed vs. for-each
- Use the indexed loop when you need the position
i, or to modify elements. - Use the for-each loop when you just need to read each value.
a[i] = 0;needs the indexed loop; summing values can use for-each.- Both visit every element — pick the one that fits the task.
Indexée vs for-each
- Utilisez la boucle indexée lorsque vous avez besoin de la position
i, ou pour modifier les éléments. - Utilisez la boucle for-each lorsque vous avez simplement besoin de lire chaque valeur.
a[i] = 0;nécessite la boucle indexée ; additionner des valeurs peut utiliser for-each.- Les deux parcourent tous les éléments — choisissez celle qui correspond à la tâche.
Bounds again
- The indexed loop's condition
i < a.lengthkeepsivalid. i <= a.lengthoverruns by one → ArrayIndexOutOfBoundsException.- The for-each loop can't go out of bounds — it handles the range for you.
- When you write the index yourself, you own the bounds.
Limites encore
- La condition de la boucle indexée
i < a.lengthgardeivalide. i <= a.lengthdépasse d'une unité → ArrayIndexOutOfBoundsException.- La boucle for-each ne peut pas sortir des limites — elle gère la plage pour vous.
- Lorsque vous écrivez l'indice vous-même, vous êtes responsable des limites.
The for-each loop reads values but can't assign back into the array. for (int x : a) { x = 0; } changes only the local copy x, not a — to zero out the array you need the indexed loop a[i] = 0;. Use for-each to read, the indexed loop to modify or when you need the position i.
La boucle for-each lit les valeurs mais ne peut pas écrire dedans. for (int x : a) { x = 0; } modifie uniquement la copie locale x, pas a — pour initialiser le tableau à zéro, il faut la boucle indexée a[i] = 0;. Utilisez for-each pour lire, la boucle indexée pour modifier ou lorsque vous avez besoin de la position i.
Two ways to sum an array:
- Indexed:
for (int i = 0; i < a.length; i++) { sum += a[i]; } - For-each:
for (int x : a) { sum += x; } - Both give the same total; for-each is cleaner when the index isn't needed.
Deux façons d'additionner un tableau :
- Indexée :
for (int i = 0; i < a.length; i++) { sum += a[i]; } - For-each :
for (int x : a) { sum += x; } - Les deux donnent le même total ; for-each est plus propre si l'indice n'est pas nécessaire.
A traversal visits every element. The indexed loop (for (int i = 0; i < a.length; i++)) gives the position i and can modify a[i]; the for-each loop (for (int x : a)) cleanly reads each value but can't change the array or use an index. Choose by whether you need the index or to modify.
Une traversée parcourt tous les éléments. La boucle indexée (for (int i = 0; i < a.length; i++)) donne la position i et peut modifier a[i] ; la boucle for-each (for (int x : a)) lit proprement chaque valeur mais ne peut pas modifier le tableau ni utiliser un indice. Choisissez selon que vous avez besoin de l'indice ou de la modification.
Traversing an array to sum it · Parcourir un tableau pour en faire la somme
i visits each index; sum accumulates a[i] (here a = {10,20,30}). · i visite chaque index ; sum accumule a[i] (ici a = {10,20,30}).
The correct condition to traverse an array a with an index is... · La condition correcte pour parcourir un tableau a avec un index est...
Valid indices are 0..length-1, so i < a.length. · Les indices valides sont 0..length-1, donc i < a.length.
Which loop should you use if you need to MODIFY the array elements? · Quelle boucle utiliser si vous devez MODIFIER les éléments du tableau ?
for-each can't assign back; use a[i] = ... with the indexed loop. · for-each ne peut pas écrire en retour ; utilisez a[i] = ... avec la boucle indexée.
In for (int x : a) { x = 0; }, the array a is set to all zeros. · Dans for (int x : a) { x = 0; }, le tableau a est mis à tous zéros.
x is a local copy; the array is unchanged. · x est une copie locale ; le tableau reste inchangé.
The for-each loop for (int x : a) is best when you only need to... · La boucle for-each for (int x : a) est meilleure quand vous avez seulement besoin de...
for-each cleanly reads values without an index. · for-each lit proprement les valeurs sans index.
Visiting every element of an array is called a ___ (one word). · Visiter chaque élément d'un tableau s'appelle un ___ (un seul mot).
A traversal visits each element. · Un parcours visite chaque élément.