在单个索引处在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个值中添加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}
并且所有这些值都存储在一个double中按以下顺序排列数组
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有一个观点,但如果你坚持使用列表,那么你可以有三个这样的arraylist:
@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);
你想要做的就是结合它们,对吧?您可以尝试这样的事情:
(我假设两个数组的大小相同,否则你需要使用两个语句)
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屋!