【发布时间】:2018-09-14 12:25:13
【问题描述】:
我正在寻找是否有更好的方法来解决我不得不使用这些签名的困境(注意:由于 Spock 测试,需要 T[][] 并且我提供 T[][] 作为数据提供者)
我的方法签名是:
public <T> T[][] createArrays(Class<T> clazz, T...items)
我正在使用具有签名的静态方法:
public static <T> Stream<Stream<T>> of(T...items)
并计算传入数组的所有排列。
我的解决方案是:手动重新输入,请原谅拼写错误:
public static <T> T[][] createArrays(Class<T> clazz, T...items){
Stream<Stream<T>> streams = EnumPerm.of(items);
List<List<T>> lists = streams.map(s -> ).collect(toList()).collect(toList());
T[][] outer = (T[][])(Array.newInstance(clazz,lists.size(),items.length);
for(int x=0;x<lists.size();x++){
List<T> innerList = lists.get(x);
for(int y=0;y<items.length;y++){
outer[x][y] = innerList.get(x);
}
}
return outer;
}
我尝试了 .map(s -> s.toArray(....)) 但无法使 T[] 强制转换工作,IntFunction 生成器工作,也无法使用 Array.newInstance 工作。我可能遗漏了一些东西,如果有任何建议,我将不胜感激。
保重并提前感谢您的宝贵时间!!
【问题讨论】:
-
如果你对未经检查的演员没问题,试试
T[][] lists = (T[][])streams.map(x -> (T[])x.toArray()).toArray(); -
感谢您的回复 Sweeper,我确实尝试过,但由于无法识别“T”的类型擦除,我遇到了问题。
标签: java arrays list java-8 java-stream