fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. List<Integer> list = new ArrayList<Integer>();
  10. list.add(1);
  11. list.add(2);
  12. list.add(3);
  13.  
  14. // .set(index, element)
  15. list.set(1, 5);
  16. System.out.println(list);
  17.  
  18. // .addAll(), .of()
  19. list.addAll(0, List.of(2, 8, 7));
  20. System.out.println(list);
  21.  
  22. // .remove(index)
  23. list.remove(3);
  24. list.add(3, 8);
  25. System.out.println(list);
  26.  
  27. // .indexOf(element)
  28. System.out.println(list.indexOf(3));
  29. // .lastIndexOf(element)
  30. System.out.println(list.lastIndexOf(8));
  31.  
  32. ListIterator<Integer> it = list.listIterator();
  33. while(it.hasNext()){
  34. System.out.print(it.next() + " ");
  35. }
  36. System.out.println();
  37.  
  38. while(it.hasPrevious()){
  39. System.out.print(it.previous() + " ");
  40. }
  41. System.out.println();
  42.  
  43. }
  44. }
Success #stdin #stdout 0.12s 19040KB
stdin
Standard input is empty
stdout
[1, 5, 3]
[2, 8, 7, 1, 5, 3]
[2, 8, 7, 8, 5, 3]
5
3
2 8 7 8 5 3 
3 5 8 7 8 2