在 ArrayList 中的单个索引处添加多个值 [英] Adding Multiple Values in ArrayList at a single index
问题描述
我在 java 中有一个双 ArrayList
像这样.
I have a double ArrayList
in java like this.
List<double[]> values = new ArrayList<double[]>(2);
现在我想做的是通过循环在列表的零索引中添加 5 个值,在索引 1 中添加 5 个值.
Now what I want to do is to add 5 values in zero index of list and 5 values in index one through looping.
第零个索引的值是 {100,100,100,100,100}
索引 1 的值是 {50,35,25,45,65}
The zeroth index would have values {100,100,100,100,100}
The index 1 would have values {50,35,25,45,65}
并且所有这些值都按照以下顺序存储在一个双数组中
and all of these values are stored in a double array in following order
double[] values = {100,50,100,35,100,25,100,45,100,65}
我该怎么做?
推荐答案
@Ahamed 有一个观点,但如果你坚持使用列表,那么你可以像这样拥有三个数组列表:
@Ahamed has a point, but if you're insisting on using lists so you can have three arraylist like this:
ArrayList<Integer> first = new ArrayList<Integer>(Arrays.AsList(100,100,100,100,100));
ArrayList<Integer> second = new ArrayList<Integer>(Arrays.AsList(50,35,25,45,65));
ArrayList<Integer> third = new ArrayList<Integer>();
for(int i = 0; i < first.size(); i++) {
third.add(first.get(i));
third.add(second.get(i));
}
如果您的列表中有以下这些值:
If you have those values on your list that below:
List<double[]> values = new ArrayList<double[]>(2);
您想做的是将它们组合起来,对吗?你可以尝试这样的事情:(我假设两个数组的大小相同,否则您需要使用两个 for 语句)
what you want to do is combine them, right? You can try something like this: (I assume that both array are same sized, otherwise you need to use two for statement)
ArrayList<Double> yourArray = new ArrayList<Double>();
for(int i = 0; i < values.get(0).length; i++) {
yourArray.add(values.get(0)[i]);
yourArray.add(values.get(1)[i]);
}
这篇关于在 ArrayList 中的单个索引处添加多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!