首页 > 解决方案 > 转换列表> 到数组列表

问题描述

所以我正在创建一个看起来像这样的方法:

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[]> 或者如果原始方法可以?

标签: javalistarraylist

解决方案


尝试这个:

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));

推荐阅读