且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 ArrayList 中的单个索引处添加多个值

更新时间:2023-12-01 12:28:22

@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]);
}