public class Solution {
public static void main(String[] args) throws Exception {
//écris ton code ici
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < 10; i++) {
String s = br.readLine();
list.add(0, s);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
I found how to put lines at the beginning of the list following the course. But i don't understand why adding the index at 0 in list.add(index, s), the printed result will begin by the end of the list ? If may someone enlighten me ?
May someone can explain to me this algorythm ?
Résolues
Commentaires (4)
- Populaires
- Nouveau
- Anciennes
Tu dois être connecté(e) pour laisser un commentaire
Lisa
20 septembre 2021, 13:10utile
if you always add the last input at the first place in a list then you get smth like this
iteration 1: list.add(0, "1"); - {1}
iteration 2: list.add(0, "2"); - {2, 1}
iteration 3: list.add(0, "3"); - {3, 2, 1}
if you now output your list in a 'default' way you'll see the reversed input
tee-hee, that's easy.... once seen 😜🤪
+1
BenOitB
20 septembre 2021, 13:28
Aaaaah ! ok i think i understood :
You mean that everytime i will enter a string (in that specific case/exercice, and 10 times), or an int or whathever, the program will put the last entry at the first place, right ?
0
Lisa
20 septembre 2021, 15:17
Yes, that's exactly what add(0, value) means. Add value at the index 0 in the list. And 0 is the first place in the list as we know ;)
Happy coding 😜🤪
+1
BenOitB
20 septembre 2021, 18:34
Thx a lot ^^ !
0