1import java.util.ArrayList;
2import java.util.Collections;
3import java.util.List;
4import java.util.ListIterator;
5public class Main {
6 public static void main(String[] args) {
7 List<String> list = new ArrayList<String>();
8 list.add("a");
9 list.add("b");
10 list.add("c");
11 list.add("d");
12 list.add("e");
13 list.add("f");
14
15 //On met la liste dans le désordre
16 Collections.shuffle(list);
17 System.out.println(list);
18
19 //On la remet dans l'ordre
20 Collections.sort(list);
21 System.out.println(list);
22
23 Collections.rotate(list, -1);
24 System.out.println(list);
25
26 //On récupère une sous-liste
27 List<String> sub = list.subList(2, 5);
28 System.out.println(sub);
29 Collections.reverse(sub);
30 System.out.println(sub);
31
32 //On récupère un ListIterator
33 ListIterator<String> it = list.listIterator();
34 while(it.hasNext()){
35 String str = it.next();
36 if(str.equals("d"))
37 it.set("z");
38 }
39 while(it.hasPrevious())
40 System.out.print(it.previous());
41
42 }
43}
44
1//Creating arraylist example
2ArrayList<String> list = new ArrayList<String>();
3
4//Adding objects in arraylist
5list.add("Mango");
6list.add("Banana");
7
8//Change the element (index,"new value")
9list.set(1,"Dates");
10
11//Return the 2nd element, because index starts from 0
12System.out.println("Returning element: " + list.get(1));
1import java.util.*;
2var list = new ArrayList<String>();
3list.add("Hello World!");