fork(4) download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Ideone {
  5. public static String join(List<String> msgs) {
  6. int size = msgs == null ? 0 : msgs.size();
  7. return size == 0 ? "" : size == 1 ? msgs.get(0) : msgs.subList(0, --size).toString().replaceAll("^.|.$", "") + " and " + msgs.get(size);
  8. }
  9.  
  10. public static void main (String[] args) throws Exception {
  11. System.out.println(join(null));
  12. System.out.println(join(Arrays.<String>asList()));
  13. System.out.println(join(Arrays.asList("one")));
  14. System.out.println(join(Arrays.asList("one", "two")));
  15. System.out.println(join(Arrays.asList("one", "two", "three")));
  16. }
  17. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout

one
one and two
one, two and three