fork download
  1. import java.util.*;
  2.  
  3. class Main {
  4. public static void main(String[] args) {
  5. List lst = new ArrayList(Arrays.asList(new Object[]{1, Arrays.asList(new Object[]{2, 3}), 4, Arrays.asList(new Object[]{ Arrays.asList(new Object[]{5, Arrays.asList(new Object[]{6}) }), 7}), }));
  6. System.out.println("" + lst);
  7. System.out.println("" + selectOdd(lst));
  8. }
  9. public static Object selectOdd(Object x) {
  10. if (x instanceof Integer) {
  11. return (Integer)x % 2 == 1 ? x : null;
  12. } else if (x instanceof List) {
  13. List lst = new ArrayList();
  14. for (Object y : (List)x) {
  15. Object z = selectOdd(y);
  16. if (z != null) {
  17. lst.add(z);
  18. }
  19. }
  20. return lst.isEmpty() ? null : lst;
  21. } else {
  22. return null;
  23. }
  24. }
  25. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
[1, [2, 3], 4, [[5, [6]], 7]]
[1, [3], [[5], 7]]