StringUtils { public static Optional<int[]> parseIntArray(String str, char c) { final int strLen; if (str == null || (strLen = str.length()) == 0) { return Optional.empty(); } try { int[] arr = new int[16]; int n = 0; int i = 0; while (i < strLen) { int j = str.indexOf(c, i); if (j < 0) { break; } if (n == arr.length) { int len = arr.length; int[] tmp = new int[len * 2]; System.arraycopy(arr, 0, tmp, 0, len); arr = tmp; } arr[n++] = Integer.parseInt(str.substring(i, j)); i = j + 1; } int[] tmp = new int[n + 1]; System.arraycopy(arr, 0, tmp, 0, n); tmp[n] = Integer.parseInt(str.substring(i)); return Optional.of(tmp); } catch (NumberFormatException ignore) { return Optional.empty(); } }  static String join(String[] a, char c); static Optional<double[]> parseDoubleArray(final String str, final char c); static Optional<int[]> parseIntArray(String str, char c);  }
@Test public void testParseIntArray() { assertFalse(StringUtils.parseIntArray(null, ',').isPresent()); assertFalse(StringUtils.parseIntArray("", ',').isPresent()); assertFalse(StringUtils.parseIntArray("foo", ',').isPresent()); assertFalse(StringUtils.parseIntArray(",123,333,652", ',').isPresent()); assertFalse(StringUtils.parseIntArray("123,333,652,", ',').isPresent()); assertArrayEquals(new int[] { 123, -333, 652 }, StringUtils.parseIntArray("123,-333,652", ',').get()); int[] arr = new int[17]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { arr[i] = i; if (i > 0) { sb.append(','); } sb.append(i); } assertArrayEquals(arr, StringUtils.parseIntArray(sb.toString(), ',').get()); }