StringUtils { public static Optional<double[]> parseDoubleArray(final String str, final char c) { final int strLen; if (str == null || (strLen = str.length()) == 0) { return Optional.empty(); } try { double[] arr = new double[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; double[] tmp = new double[len * 2]; System.arraycopy(arr, 0, tmp, 0, len); arr = tmp; } arr[n++] = Double.parseDouble(str.substring(i, j)); i = j + 1; } double[] tmp = new double[n + 1]; System.arraycopy(arr, 0, tmp, 0, n); tmp[n] = Double.parseDouble(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 testParseDoubleArray() { assertFalse(StringUtils.parseDoubleArray(null, ',').isPresent()); assertFalse(StringUtils.parseDoubleArray("", ',').isPresent()); assertFalse(StringUtils.parseDoubleArray("foo", ',').isPresent()); assertFalse(StringUtils.parseDoubleArray(",123.0,333.0,652.0", ',').isPresent()); assertFalse(StringUtils.parseDoubleArray("123.0,333.0,652.0,", ',').isPresent()); assertArrayEquals(new double[] { 123.0, -333.0, 652.0 }, StringUtils.parseDoubleArray("123.0,-333.0,652", ',').get(), 0.0); double[] arr = new double[17]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { double factor = i % 2 == 0 ? -2.0 : 2.0; double d = factor * i; arr[i] = d; if (i > 0) { sb.append(','); } sb.append(d); } assertArrayEquals(arr, StringUtils.parseDoubleArray(sb.toString(), ',').get(), 0.0); }