英文:
How to create two-dimensional array based on two list
问题
我有这样的类:
@Data
public class Pair {
private List<Long> firstCurrency; // 1, 5, 7, 2
private List<Long> secondCurrency; // 2 , 9 , 11 , 5
}
基于这两个列表,我想创建这样的类:
@Data
public class Pair {
private List<OwnPair> pairs;
}
其中 OwnPair
为:
@Data
public class OwnPair {
private Long timestamps;
private Long values;
}
因此,最后我将在 pairs
中有 4 个对象:((1,2) , (5,9), (7,9) , (2,5))
注意:
这两个列表的大小始终相同,索引必须与顺序匹配。我遇到问题是因为我不知道如何同时迭代两个列表。
我应该如何表达这两个列表。
英文:
I have such class:
@Data
public class Pair {
private List<Long> firstCurrency; // 1, 5, 7, 2
private List<Long> secondCurrency; // 2 , 9 , 11 , 5
}
Based on those two list I would like to create such class:
@Data
public class Pair {
private List<OwnPair> pairs;
}
where OwnPair:
@Data
public class OwnPair {
private Long timestamps;
private Long values;
}
so, at the and i will have 4 object inside of pairs (( 1,2) , (5,9), (7,9) , (2,5))
Note :
The two lists will always be the same size, and the indexes just have to match the order. I have a problem because i don't know how to iterate over two lists at once.
How i could spell the two lists.
答案1
得分: 1
如果您的大小无论如何都已确定(如果您指的是“相同大小”),您还可以使用数组,因为只有在条目数量未确定时,列表才有意义。
long[] firstCurrency = {1,5,7,2};
long[] secondCurrency = {2,9,11,5};
long[][] merged = new long[firstCurrency.length][2];
for(int i = 0; i<firstCurrency.length; i++)
{
merged[i][0] = firstCurrency[i];
merged[i][1] = secondCurrency[i];
}
System.out.println(Arrays.deepToString(merged));
输出:[[1, 2], [5, 9], [7, 11], [2, 5]]
如果您真的想使用列表,只需将 .length
替换为 .size()
,将数组访问替换为 .get(i)
。
此外,没有必要将此包装到独立的类中。
英文:
If your size is determined anyway (if you meant that by "same size"), you can also use arrays, because Lists only make sense when the amount of entries is not determined.
long[] firstCurrency = {1,5,7,2};
long[] secondCurrency = {2,9,11,5};
long[][] merged = new long[firstCurrency.length][2];
for(int i = 0; i<firstCurrency.length; i++)
{
merged[i][0] = firstCurrency[i];
merged[i][1] = secondCurrency[i];
}
System.out.println(Arrays.deepToString(merged));
outputs: [[1, 2], [5, 9], [7, 11], [2, 5]]
If you really want to use lists though, just replace the .length
with .size()
and the array access with .get(i)
.
Also, there is no need to wrap this into an own class.
答案2
得分: 0
一个基于流的方法是在列表的大小上创建一个 IntStream
。这是在假设两个列表具有相同大小的情况下:
List<OwnPair> collect = IntStream.range(0, firstCurrency.size())
.mapToObj(i -> new OwnPair(firstCurrency.get(i), secondCurrency.get(i)))
.collect(Collectors.toList());
range()
创建一个从 0 到 3 的 IntStream
,然后对于每个索引,mapToObj()
将索引映射到一个 OwnPair
对象。在这里,假设存在一个接受这两个值的构造函数。然后将结果收集到一个 List
中,当然可以根据需要更改为其他形式。
英文:
A stream based approach would be to create an IntStream
over the size of the lists. This is assuming that both the lists have the same size:
List<OwnPair> collect = IntStream.range(0, firstCurrency.size())
.mapToObj(i -> new OwnPair(firstCurrency.get(i), secondCurrency.get(i)))
.collect(Collectors.toList());
range()
is creating an IntStream
ranging from 0 to 3, then for each index mapToObj()
maps the index to an OwnPair
. Here, a constructor accepting the two values is assumed. The result is then collected to a List
but can of course be changed to whatever is needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论