英文:
How to create a list of list of given size and initialize with values in java
问题
这是用C++初始化具有一些值和给定大小的列表的代码:
vector<vector<int>> arr = vector<>(rowSize, vector<>(colSize, value));
在Java中是否有相等的代码?
我们可以使用IntStream.range()
来流式处理并创建新的列表,并将其添加到主列表中。然后循环遍历2D列表并设置一些默认值。
我正在寻找类似于上述C++实现的东西。
英文:
This is the code to initialize a list of list with some values and given size in C++
vector<vector<int>> arr = vector<>(rowSize, vector<>(colSize, value));
Is there any equivalent code in Java?
We could use the IntStream.range() to stream and create new List and add it to the main list. Then loop through the 2D list and set some default values.
I'm searching for something similar to c++ implementation mentioned above
答案1
得分: 1
你可以这样做:
Supplier<List<Integer>> s = Stream.generate(() -> value)
.limit(colSize)
.collect(toCollection(ArrayList::new));
List<List<Integer>> table = Stream.generate(s)
.limit(rowSize)
.collect(toCollection(ArrayList::new));
但是对于这个任务来说,使用Stream
可能有点过度:
List<Integer> columnTemplate = Collections.nCopies(colSize, value);
List<List<Integer>> table = new ArrayList<>(rowSize);
for (int i = 0; i < rowSize; ++i) {
table.add(new ArrayList<>(columnTemplate)); // 因为列表是不可变的
}
如你所见,我使用了不返回不可变集合的版本:你可能想要能够更改某行/列的值:
table.get(row).set(col, n);
如果直接在Stream
或nCopies
上使用toList()
,这将失败。
英文:
You could do that:
Supplier<List<Integer>> s = Stream.generate(() -> value)
.limit(colSize)
.collect(toCollection(ArrayList::new));
List<List<Integer>> table = Stream.generate(s)
.limit(rowSize)
.collect(toCollection(ArrayList::new));
But it may be overkill to use Stream
for that:
List<Integer> columnTemplate = Collections.nCopies(colSize, value);
List<List<Integer>> table = new ArrayList<>(rowSize);
for (int i = 0; i < rowSize; ++i) {
table.add(new ArrayList<>(columnTemplate)); // because list is immutable
}
As you can see, I used version that do no return immutable collection: you would probably want to be able to change the value at some row/col:
table.get(row).set(col, n);
This will fail if you use toList()
on Stream
or nCopies
directly.
答案2
得分: 0
你可以使用 Collections.nCopies
来创建一个包含特定值的原始类型 List
。要创建包含多个 List
的 List
,你可以使用 Stream.generate
(确保每行都是一个单独的引用)。
例如:
int value = 1, rowSize = 3, colSize = 10;
List<List<Integer>> list = Stream.generate(() ->
(List<Integer>)new ArrayList<>(Collections.nCopies(colSize, value)))
.limit(rowSize).collect(Collectors.toCollection(ArrayList::new));
如果你有其他问题或需要进一步的解释,请随时提出。
英文:
You can use Collections.nCopies
for a List
of primitives filled with a certain value. To create the List
of List
s, you can use Stream.generate
(to ensure that each row is a separate reference).
For example:
int value = 1, rowSize = 3, colSize = 10;
List<List<Integer>> list = Stream.generate(() ->
(List<Integer>)new ArrayList<>(Collections.nCopies(colSize, value)))
.limit(rowSize).collect(Collectors.toCollection(ArrayList::new));
答案3
得分: 0
你有两种方法可以做到这一点,两者都需要一个泛型类型的值。
传统的for循环或使用流。
注意: Collections.nCopies
方法返回一个不可变列表,所以如果你打算修改它,你需要将它传递给 ArrayList
构造函数。
import java.util.*;
import java.util.stream.*;
public class Matrix {
public static void main(String[] args) {
List<List<Integer>> intMatrix1 = initializeMatrix(3, 4, 1);
List<List<Integer>> intMatrix2 = initializeMatrix2(3, 4, 1);
System.out.println(intMatrix1); // [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
System.out.println(intMatrix2); // [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
}
public static <E> List<List<E>> initializeMatrix(int rows, int cols, E value) {
List<List<E>> matrix = new ArrayList<>();
for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
List<E> row = new ArrayList<>();
for (int colIndex = 0; colIndex < cols; colIndex++) {
row.add(colIndex, value);
}
matrix.add(row);
}
return matrix;
}
public static <E> List<List<E>> initializeMatrix2(int rows, int cols, E value) {
return Stream.generate(() -> new ArrayList<>(Collections
.nCopies(cols, value)))
.limit(rows)
.collect(Collectors.toList());
}
}
英文:
You have two ways of doing this, and both take a generic type for the value.
Traditional for loop, or with streams.
Note: The Collections.nCopies
method returns an immutable list, so you will need to pass it to the ArrayList
constructor if you intend to modify it.
import java.util.*;
import java.util.stream.*;
public class Matrix {
public static void main(String[] args) {
List<List<Integer>> intMatrix1 = initializeMatrix(3, 4, 1);
List<List<Integer>> intMatrix2 = initializeMatrix2(3, 4, 1);
System.out.println(intMatrix1); // [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
System.out.println(intMatrix2); // [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
}
public static <E> List<List<E>> initializeMatrix(int rows, int cols, E value) {
List<List<E>> matrix = new ArrayList<>();
for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
List<E> row = new ArrayList<>();
for (int colIndex = 0; colIndex < cols; colIndex++) {
row.add(colIndex, value);
}
matrix.add(row);
}
return matrix;
}
public static <E> List<List<E>> initializeMatrix2(int rows, int cols, E value) {
return Stream.generate(() -> new ArrayList<>(Collections
.nCopies(cols, value)))
.limit(rows)
.collect(Collectors.toList());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论