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_OPEN_TAG = "["; /** * */ private static final String ARRAY_CLOSE_TAG = "]"; /** * * @param value * @return int[] */ public static int[] toIntArray(final String value) { if (value == null) { return new int[] {}; } final String strippedValue = value.replace(ARRAY_OPEN_TAG, EMPTY_STRING).replace(ARRAY_CLOSE_TAG, EMPTY_STRING); final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR); final Collection<Integer> intCollection = new ArrayList<>(); while (tokenizer.hasMoreTokens()) { intCollection.add(Integer.valueOf(tokenizer.nextToken().trim())); } return toIntArray(intCollection); } /** * * @param collection * @return int[] */ public static int[] toIntArray(final Collection<Integer> collection) { final int[] array = new int[collection == null ? 0 : collection.size()]; if (collection != null) { int i = 0; for (Iterator<Integer> iterator = collection.iterator(); iterator.hasNext();) { array[i++] = iterator.next().intValue(); } } return array; } }