Adding Multiple Values in ArrayList at a single index

前端 未结 7 455
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 01:35

I have a double ArrayList in java like this.

List values = new ArrayList(2);

Now what I want t

7条回答
  •  被撕碎了的回忆
    2021-01-12 01:52

    @Ahamed has a point, but if you're insisting on using lists so you can have three arraylist like this:

    ArrayList first = new ArrayList(Arrays.AsList(100,100,100,100,100));
    ArrayList second = new ArrayList(Arrays.AsList(50,35,25,45,65));
    ArrayList third = new ArrayList();
    
    for(int i = 0; i < first.size(); i++) {
          third.add(first.get(i));
          third.add(second.get(i));
    }
    

    Edit: If you have those values on your list that below:

    List values = new ArrayList(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 yourArray = new ArrayList();
    for(int i = 0; i < values.get(0).length; i++) {
        yourArray.add(values.get(0)[i]);
        yourArray.add(values.get(1)[i]);
    }
    

提交回复
热议问题