fork(1) download
  1. import java.util.Arrays;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. String s = "1 34 5 23 7 89 32 4 10 3 6 5";
  6. System.out.println(Arrays.toString(string2array(s)));
  7. }
  8.  
  9. public static int[] string2array(String s) {
  10. return util1(s, 0, 0, false);
  11. }
  12.  
  13. private static int[] util1(String s, int n, int l, boolean b) {
  14. if (s.isEmpty()) {
  15. return b ? util2(l, n, new int[l + 1]) : new int[l];
  16. }
  17. if (Character.isWhitespace(s.charAt(0))) {
  18. return b ? util2(l, n, util1(s.substring(1), 0, l + 1, false)) : util1(s.substring(1), 0, l, false);
  19. }
  20. return util1(s.substring(1), n * 10 + Character.digit(s.charAt(0), 10), l, true);
  21. }
  22.  
  23. private static int[] util2(int idx, int value, int[] array) {
  24. array[idx] = value;
  25. return array;
  26. }
  27. }
  28.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
[1, 34, 5, 23, 7, 89, 32, 4, 10, 3, 6, 5]