java - 转换列表> 到数组列表
问题描述
所以我正在创建一个看起来像这样的方法:
public static ArrayList<int[]> permuteArray(int[] array)
我有一个看起来像这样的辅助方法:
public static List<List<Integer>> permuteArray(List<List<Integer>> list, List<Integer> result, int [] arr) {
if(result.size() == arr.length){
list.add(new ArrayList<>(result));
}
else{
for(int i = 0; i < arr.length; i++){
if(result.contains(arr[i]))
{
continue;
}
result.add(arr[i]);
permuteArray(list, result, arr);
result.remove(result.size() - 1);
}
}
return list;
}
我有这一行:List<List<Integer>> permute = permuteArray(list, new ArrayList<>(), array);
但我想将其转换List<List<Integer>>
为ArrayList<int[]>
. 辅助方法是否可以返回 ArrayList<int[]> 或者如果原始方法可以?
解决方案
尝试这个:
List<List<Integer>> lst = new ArrayList<>();
lst.add(List.of(1, 2, 3));
lst.add(List.of(4, 5, 6));
lst.add(List.of(7,8,9));
ArrayList<int[]> newList = lst.stream()
.map(x -> x.stream().mapToInt(k -> k).toArray())
.collect(Collectors.toCollection(ArrayList::new));
推荐阅读
- combobox - 组合框聚合绑定仅显示最后一项
- web3js - 未捕获(承诺中)错误:返回的值无效,它是否耗尽了 Gas?
- amazon-web-services - 带有附件的 AWS-Pinpoint 电子邮件通知不起作用
- java - Clojure 中的迭代函数参数
- c# - 提交 HTML 表单并下载文件
- python - Pandas groupby sum,在结果数据框中保留特定列
- java - Springboot UnsatisfiedDependencyException?
- express - 快速获取请求承诺
- swiftui - SwiftUI:如何在按钮单击时关闭(不是模态)子视图?
- python - 减少网络服务器中的 I/O 延迟——在 python 中使用 select 时是否需要 asyncio?