fork download
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.function.Supplier;
  4.  
  5. class SubListMadness {
  6. public static void main(String[] args) {
  7. printResult(() -> exampleList().subList(0,3));
  8. printResult(() -> exampleList().subList(0,4));
  9. printResult(() -> exampleList().subList(-1,4));
  10. printResult(() -> exampleList().subList(-1,5));
  11. printResult(() -> exampleList().subList(0,5));
  12. printResult(() -> exampleList().subList(5,5));
  13. printResult(() -> exampleList().subList(5,4));
  14. printResult(() -> exampleList().subList(4,4)); //WTF?!
  15. }
  16.  
  17. private static List<String> exampleList() {
  18. List<String> list = new ArrayList<>();
  19. list.add("a");
  20. list.add("b");
  21. list.add("c");
  22. list.add("d");
  23. return list;
  24. }
  25.  
  26. private static <T> void printResult(Supplier<T> s) {
  27. try {
  28. System.out.println(s.get());
  29. } catch (Exception e) {
  30. System.out.println(e.getClass() + " " + e.getMessage());
  31. }
  32. }
  33. }
  34.  
Success #stdin #stdout 0.14s 2184192KB
stdin
Standard input is empty
stdout
[a, b, c]
[a, b, c, d]
class java.lang.IndexOutOfBoundsException fromIndex = -1
class java.lang.IndexOutOfBoundsException fromIndex = -1
class java.lang.IndexOutOfBoundsException toIndex = 5
class java.lang.IndexOutOfBoundsException toIndex = 5
class java.lang.IllegalArgumentException fromIndex(5) > toIndex(4)
[]