package com.conorgriffin.flattener; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Flattens an array of arbitrarily nested arrays of integers into a flat array of integers. * * @author conorgriffin */ public class IntegerArrayFlattener { /** * Flatten an array of arbitrarily nested arrays of integers into a flat array of integers. eg [[1,2,[3]],4] -> [1,2,3,4]. * * @param inputArray an array of Integers or nested arrays of Integers * @return flattened array of Integers or null if input is null * @throws IllegalArgumentException */ public static Integer[] flatten(Object[] inputArray) throws IllegalArgumentException { if (inputArray == null) return null; List flatList = new ArrayList(); for (Object element : inputArray) { if (element instanceof Integer) { flatList.add((Integer) element); } else if (element instanceof Object[]) { flatList.addAll(Arrays.asList(flatten((Object[]) element))); } else { throw new IllegalArgumentException("Input must be an array of Integers or nested arrays of Integers"); } } return flatList.toArray(new Integer[flatList.size()]); } }
unit testing:
package com.conorgriffin.flattener; import org.junit.Assert; import org.junit.Test; /** * Tests IntegerArrayFlattener */ public class IntegerArrayFlattenerTest { Integer[] expectedArray = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; @Test public void testNullReturnsNull() throws IllegalArgumentException { Assert.assertNull( "Testing a null argument", IntegerArrayFlattener.flatten(null) ); } @Test public void testEmptyArray() throws IllegalArgumentException { Assert.assertArrayEquals( "Testing an empty array", new Integer[]{}, IntegerArrayFlattener.flatten(new Object[]{}) ); } @Test public void testFlatArray() throws IllegalArgumentException { Assert.assertArrayEquals( "Testing a flat array", expectedArray, IntegerArrayFlattener.flatten(new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) ); } @Test public void testNestedArray() throws IllegalArgumentException { Assert.assertArrayEquals( "Testing nested array", expectedArray, IntegerArrayFlattener.flatten(new Object[]{1, 2, 3, 4, new Object[]{5, 6, 7, 8}, 9, 10}) ); } @Test public void testMultipleNestedArrays() throws IllegalArgumentException { Assert.assertArrayEquals( "Testing multiple nested arrays", expectedArray, IntegerArrayFlattener.flatten(new Object[]{1, 2, new Object[]{3, 4, new Object[]{5}, 6, 7}, 8, 9, 10}) ); } @Test(expected = IllegalArgumentException.class) public void throwsExceptionForObjectInArray() throws IllegalArgumentException { IntegerArrayFlattener.flatten( new Object[]{new Object()} ); } @Test(expected = IllegalArgumentException.class) public void throwsExceptionForObjectInNestedArray() throws IllegalArgumentException { IntegerArrayFlattener.flatten( new Object[]{1, 2, new Object[]{3, new Object()}} ); } @Test(expected = IllegalArgumentException.class) public void throwsExceptionForNullInArray() throws IllegalArgumentException { IntegerArrayFlattener.flatten( new Object[]{null} ); } @Test(expected = IllegalArgumentException.class) public void throwsExceptionForNullInNestedArray() throws IllegalArgumentException { IntegerArrayFlattener.flatten( new Object[]{1, 2, new Object[]{3, null}} ); } }
String a = "[[[1],2],[3]],4] "; a= a.replaceAll("[(\\[|\\])]", ""); String[] b = a.split(",");
这就是我在Java中解决这个问题的方法:
public class ArrayUtil { /** * Utility to flatten an array of arbitrarily nested arrays of integers into * a flat array of integers. eg [[1,2,[3]],4] -> [1,2,3,4] * @param inputList */ public static Integer[] flattenArray(ArrayList
它可以通过迭代方法展平。
static class ArrayHolder implements Iterator { private final Object[] elements; private int index = -1; public ArrayHolder(final Object[] elements) { this.elements = elements; } @Override public boolean hasNext() { return Objects.nonNull(elements) && ++index < elements.length; } @Override public Object next() { if (Objects.isNull(elements) || (index == -1 || index > elements.length)) throw new NoSuchElementException(); return elements[index]; } } private static boolean hasNext(ArrayHolder current) { return Objects.nonNull(current) && current.hasNext(); } private void flat(Object[] elements, List flattened) { Deque stack = new LinkedList<>(); stack.push(new ArrayHolder(elements)); ArrayHolder current = null; while (hasNext(current) || (!stack.isEmpty() && hasNext(current = stack.pop()))) { Object element = current.next(); if (Objects.nonNull(element) && element.getClass().isArray()) { Object[] e = (Object[]) element; stack.push(current); stack.push(new ArrayHolder(e)); current = null; } else { flattened.add(element); } } }
你可以在这里找到完整的源代码你可以使用递归来解决这个问题。
private void flat(Object[] elements, List flattened) { for (Object element : elements) { if (Objects.nonNull(element) && element.getClass().isArray()) { flat((Object[])element, flattened); } else { flattened.add(element); } } }