我想扁平化嵌套数组,如下所示:
[[[1],2],[3]],4] -> [1,2,3,4] 在java中手动找不到线索!:S
我已经尝试了一个手动的java脚本指南,但是没有得到解决方案。
发布于 2017-07-04 23:32:37
Java 8的Stream API提供了一个紧凑而灵活的解决方案。使用该方法
private static Stream<Object> flatten(Object[] array) {
return Arrays.stream(array)
.flatMap(o -> o instanceof Object[]? flatten((Object[])o): Stream.of(o));
}您可以按如下方式执行操作
Object[] array = { 1, 2, new Object[]{ 3, 4, new Object[]{ 5 }, 6, 7 }, 8, 9, 10 };
System.out.println("original: "+Arrays.deepToString(array));
Object[] flat = flatten(array).toArray();
System.out.println("flat: "+Arrays.toString(flat));或者假设叶对象为特定类型时:
int[] flatInt = flatten(array).mapToInt(Integer.class::cast).toArray();
System.out.println("flat int: "+Arrays.toString(flat));发布于 2016-02-16 21:25:41
我使用Java创建了a class to solve this,代码如下所示。
解决方案:
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.
* <p/>
* @author conorgriffin
*/
public class IntegerArrayFlattener {
/**
* Flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[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<Integer> flatList = new ArrayList<Integer>();
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()]);
}
}单元测试:
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}}
);
}
}发布于 2018-09-18 22:45:55
这就是我解决这个问题的方法。不知道你想要的是哪种效率。不过,是的。它可以在JavaScript中完成这项工作。
arr.toString().split(',').filter((item) => item).map((item) => Number(item))
更有效的方法可能是使用arr和递归中的reduce和concat方法。
function flattenDeep(arr1) {
return arr1.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}https://stackoverflow.com/questions/31851548
复制相似问题