英文:
How to convert an 2D array specifically or any array into List<List<Integer>> in java 8?
问题
return Arrays.stream(a)
.map(row -> Arrays.asList(row))
.collect(Collectors.toList());
英文:
Suppose there is an array Integer[][] a;
Then how to convert this a
into List<List<Integer>>
in java 8?
This is my code of leetcode Pascal's trianagle
class Solution {
public List<List<Integer>> generate(int numRows) {
Integer[][] a = new Integer[numRows][numRows];
a[0][0] = 1;
for(int i=0;i<numRows;i++) {
a[i][0] = 1;
for(int j=0;j<=i;j++) {
if(i == j)
a[i][j] = 1;
else {
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
return ______;
}
}
Now what should i write in return statement?
答案1
得分: 5
这将会过去。我们将使用类型为 List<List<Integer>>
(所需的输出类型)的 ArrayList<>()
来存储所有行,而临时行只使用 ArrayList<>()
:
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> rows = new ArrayList<>();
ArrayList<Integer> row = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
row.add(0, 1);
for (int j = 1; j < row.size() - 1; j++)
row.set(j, row.get(j) + row.get(j + 1));
rows.add(new ArrayList<Integer>(row));
}
return rows;
}
}
此外,还有一个关于 Pascal's Triangle 问题的数学解法(例如,在 Python 中):
import math
class Solution:
def generate(self, size):
if size < 0:
return [[1]]
rows = []
for n in range(size):
row = []
for r in range(n + 1):
row.append(math.factorial(n) // math.factorial(r) // math.factorial(n - r))
rows.append(row)
return rows
参考资料:
英文:
This'll pass. We'd use an ArrayList<>()
of type List<List<Integer>
(which is the desired output) for all rows and just an ArrayList<>()
for the temp row:
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> rows = new ArrayList<>();
ArrayList<Integer> row = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
row.add(0, 1);
for (int j = 1; j < row.size() - 1; j++)
row.set(j, row.get(j) + row.get(j + 1));
rows.add(new ArrayList<Integer>(row));
}
return rows;
}
}
There is also a math Solution for Pascal's Triangle problem (e.g, in Python):
import math
class Solution:
def generate(self, size):
if size < 0:
return [[1]]
rows = []
for n in range(size):
row = []
for r in range(n + 1):
row.append(math.factorial(n) // math.factorial(r) // math.factorial(n - r))
rows.append(row)
return rows
References
答案2
得分: 2
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6, 7, 8, 9 }, { 10, 11, 12, 13 } };
List<List<Integer>> list1 = Arrays.stream(arr)
.map(ar -> Arrays.stream(ar).boxed()
.collect(Collectors.toList()))
.collect(Collectors.toList());
List<List<Integer>> list2 = Arrays.stream(arr)
.map(ar -> Arrays.stream(ar).boxed().collect(
Collectors.toCollection(ArrayList::new)))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(list1);
System.out.println(list2);
Prints
[[1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13]]
[[1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13]]
英文:
>how to convert a 2D array specifically or any array into List<List<Integer>> in java 8?
If you use lists to begin with you wouldn't need to do the conversion. But to answer your question for perhaps future reference, here is how you do it.
- A 2D array is an array of arrays.
- So the first stream, streams the arrays.
- the inner stream, streams the individual arrays and collects the values into a list.
- those lists are then once again collected into another List.
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6, 7, 8, 9 },
{ 10, 11, 12, 13 } };
The first method returns the default implementation of list.
List<List<Integer>> list1 = Arrays.stream(arr)
.map(ar -> Arrays.stream(ar).boxed()
.collect(Collectors.toList()))
.collect(Collectors.toList());
This method allows you to specify the collection
type - in this case, an array list.
List<List<Integer>> list2 = Arrays.stream(arr)
.map(ar -> Arrays.stream(ar).boxed().collect(
Collectors.toCollection(ArrayList::new)))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(list1);
System.out.println(list2);
Prints
[[1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13]]
[[1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13]]
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论