如何解决在 Java 8 中将嵌套的 int 数组转换为带有流的列表列表
public class MyClass {
public static void main(String args[]) {
int[][] arrayMatrix= {{6, 7, 20}, {2, 0, 30}, {7, 20, 15}};
List<List<Integer>> listMatrix = Arrays.stream(arrayMatrix)
.map(array -> Arrays.stream(array).boxed()
.collect(Collectors.toList()))
.collect(Collectors.toList());
System.out.println("List Matrix :"+listMatrix);
}
}
Booking Matrix :[[6, 7, 20], [2, 0, 30], [7, 20, 15]]
解决方法
我有一个数组:
int[][] bookings= {{6,7,20},{2,30},{7,20,15}};
我想成为:
ArrayList = {
ArrayList<Integer>{6,ArrayList<Integer>{2,ArrayList<Integer>{7,15},}
我怎样才能做到这一点?
我试过使用:
Arrays.stream(bookings)
.flatMapToInt(x -> Arrays.stream(x))
.boxed()
.collect(Collectors.toList());
但这仅返回一个Arraylist
,例如:
ArrayList list = {6,2,30,15};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。