fork download
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.stream.*;
  4.  
  5. class Main {
  6. public static void main(String[] args) {
  7. String [] data = {"europe", "france", "germany", "america"};
  8. int startIndex = 1;
  9. int count = 2;
  10. int newSize = data.length - count + 1;
  11.  
  12. List<String> output =
  13. IntStream.range(0, newSize)
  14. .mapToObj(n -> Main.getOrJoin(data, startIndex, count, n))
  15. .collect(Collectors.toList());
  16.  
  17. for (String s : output) {
  18. System.out.println(s);
  19. }
  20. }
  21.  
  22. private static String getOrJoin(String[] data, int joinIndex, int count, int index) {
  23. if (index < joinIndex) return data[index];
  24. else if (index > joinIndex) return data[index + count - 1];
  25. else {
  26. String[] dataToJoin = Arrays.copyOfRange(data, joinIndex, joinIndex + count);
  27. return String.join(" ", dataToJoin);
  28. }
  29. }
  30. }
Success #stdin #stdout 0.28s 33448KB
stdin
Standard input is empty
stdout
europe
france germany
america