Here's a way to do it with strings. Not particularly performant, but (I hope) easy to understand.
package playground.tests;
import junit.framework.TestCase;
public class SplitIntegerTest extends TestCase {
public void testIntsFromOneInteger() throws Exception {
assertEqualArrays(new int[] { 1, 0, 2, 1, 2 }, intsFrom(10212));
}
public void testIntsFromArray() throws Exception {
int test[] = { 10212, 10202, 11000, 11000, 11010 };
int test2[] = { 1, 0, 2, 1, 2, 1, 0, 2, 0, 2, 1, 1, 0, 0, 0, 1, 1, 0,
0, 0, 1, 1, 0, 1, 0 };
assertEqualArrays(test2, intsFrom(test));
}
private int[] intsFrom(int[] input) {
int[][] list = new int[input.length][];
for (int i = 0; i < input.length; i++)
list[i] = intsFrom(input[i]);
int n = 0;
for (int[] array : list)
n += array.length;
int[] result = new int[n];
int index = 0;
for (int[] array : list)
for (int i : array)
result[index++] = i;
return result;
}
private static int[] intsFrom(Integer n) {
String s = n.toString();
int[] result = new int[s.length()];
for (int i = 0; i < result.length; i++)
result[i] = intFrom(s.charAt(i));
return result;
}
private static int intFrom(Character c) {
return Integer.parseInt(c.toString());
}
private static void assertEqualArrays(int[] a, int[] b) {
assertEquals(a.length, b.length);
for (int i = 0; i < b.length; i++)
assertEquals(a[i], b[i]);
}
}