Java tutorial
//package com.java2s; /******************************************************************************* * Manchester Centre for Integrative Systems Biology * University of Manchester * Manchester M1 7ND * United Kingdom * * Copyright (C) 2007 University of Manchester * * This program is released under the Academic Free License ("AFL") v3.0. * (http://www.opensource.org/licenses/academic.php) *******************************************************************************/ import java.util.*; public class Main { /** * */ public static final String ELEMENT_SEPARATOR = ",(\\s)*"; /** * */ private static final String EMPTY_STRING = ""; /** * */ private static final String ARRAY_SEPARATOR = ";"; /** * */ private static final String ARRAY_OPEN_TAG = "["; /** * */ private static final String ARRAY_CLOSE_TAG = "]"; /** * * @param value * @return double[][] */ public static double[][] to2dDoubleArray(final String value) { final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG, EMPTY_STRING); final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ARRAY_SEPARATOR); final double[][] array = new double[tokenizer.countTokens()][]; int i = 0; while (tokenizer.hasMoreTokens()) { array[i++] = toDoubleArray(tokenizer.nextToken()); } return array; } /** * * @param collection * @return double[][] */ public static double[][] to2dDoubleArray(final List<double[]> collection) { final double[][] array = new double[collection.size()][]; int i = 0; for (Iterator<double[]> iterator = collection.iterator(); iterator.hasNext();) { array[i++] = iterator.next(); } return array; } /** * * @param value * @return double[] */ public static double[] toDoubleArray(final String value) { if (value == null) { return new double[] {}; } final int BRACKET_LENGTH = 1; final String strippedValue = value.substring(BRACKET_LENGTH, value.length() - BRACKET_LENGTH); final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR); final Collection<Double> doubleCollection = new ArrayList<>(); while (tokenizer.hasMoreTokens()) { doubleCollection.add(Double.valueOf(tokenizer.nextToken().trim())); } return toDoubleArray(doubleCollection); } /** * * @param stringArray * @return double[] */ public static double[] toDoubleArray(final String[] stringArray) { final Collection<Double> doubleCollection = new ArrayList<>(); for (String value : stringArray) { doubleCollection.add(Double.valueOf(value)); } return toDoubleArray(doubleCollection); } /** * * @param doubleArray * @return double[] */ public static double[] toDoubleArray(final Double[] doubleArray) { return toDoubleArray(Arrays.asList(doubleArray)); } /** * * @param collection * @return double[] */ public static double[] toDoubleArray(final Collection<Double> collection) { final double[] array = new double[collection == null ? 0 : collection.size()]; if (collection != null) { int i = 0; for (Iterator<Double> iterator = collection.iterator(); iterator.hasNext();) { array[i++] = iterator.next().doubleValue(); } } return array; } }