[英]Converting List<List<Integer>> to ArrayList<int[]>
So i'm creating a method that looks like this:所以我正在创建一个看起来像这样的方法:
public static ArrayList<int[]> permuteArray(int[] array)
I have a helper method that looks like this:我有一个看起来像这样的辅助方法:
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;
}
I have this line: List<List<Integer>> permute = permuteArray(list, new ArrayList<>(), array);
我有这一行:
List<List<Integer>> permute = permuteArray(list, new ArrayList<>(), array);
But i want to convert the List<List<Integer>>
to ArrayList<int[]>
.但我想将
List<List<Integer>>
转换为ArrayList<int[]>
。 Is this possible for the helper method to return ArrayList<int[]> or if the original method can?辅助方法是否可以返回 ArrayList<int[]> 或者如果原始方法可以?
Try this:尝试这个:
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));
While modifying the original method might be the better way to go, this is the answer to the title, which is 'Converting List<List<Integer>>
to ArrayList<int[]>
'虽然修改原始方法可能是 go 的更好方法,但这是标题的答案,即“将
List<List<Integer>>
转换为ArrayList<int[]>
”
List<List<Integer>> original = ...;
ArrayList<int[]> arraylist = original.stream()
.map(sub -> sub.stream().mapToInt(i -> i).toArray())
.collect(Collectors.toCollection(ArrayList::new));
You can also try using the below to convert List<List> to ArrayList<int[]>,您也可以尝试使用以下方法将 List<List> 转换为 ArrayList<int[]>,
list.stream().map(x -> x.stream().mapToInt(Integer::intValue).toArray())
.collect(Collectors.toCollection(ArrayList::new));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.