[英]Convert List of Lists to int array
I have input of list of lists which i want to convert into int array which can help for my logic我输入了列表列表,我想将其转换为 int 数组,这有助于我的逻辑
list of Lists "lst" has input [[1,0,1],[1,1,0],[0,0,1]]
列表“lst”的列表有输入[[1,0,1],[1,1,0],[0,0,1]]
output array should be like {{1,0,1},{1,1,0},{0,0,1}}
output 数组应该像{{1,0,1},{1,1,0},{0,0,1}}
int[] arr = new int[lst.size()];
for(int i=0;i<lst.size();i++){
for(int j=0;j<lst.size();j++){
arr[i] = lst.get(i).get(j);
}
}
Here are two ways.这里有两种方法。
The data数据
List<List<Integer>> list = List.of(List.of(1,0,1),
List.of(1,1,0),
List.of(0,0,1));
int[][] arr = new int[list.size()][];
for (int i = 0; i < list.size(); i++) {
List<Integer> lst = list.get(i);
int [] temp = new int[lst.size()];
for (int k = 0; k < lst.size(); k++) {
temp[k] = lst.get(k);
}
arr[i] = temp;
}
System.out.println(Arrays.deepToString(arr));
Or或者
int[][] arr = list.stream()
.map(ls->ls.stream().mapToInt(a->a).toArray())
.toArray(int[][]::new);
System.out.println(Arrays.deepToString(arr));
Both print都打印
[[1, 0, 1], [1, 1, 0], [0, 0, 1]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.