从ArrayList添加到给定索引的另一个ArrayList

时间:2014-03-29 11:47:27

标签: java arraylist

我有两个arraylist

private final ArrayList children = new ArrayList();
private final ArrayList values = new ArrayList();

我有一个方法,当使用值(索引号)调用时,应填充 children arrayList,从给定索引i开始的 ArrayList中取值并填充它是循环的。

enter image description here

private void populateList(int i)
{
    children.clear();
    // A logic to add list in this form as shown in the above picture.
    children.add(values.get(i));
    children.add(values.get(i + 1));
    ...
}

我需要一个逻辑,将值arrayList中的复制到 children arrayList,并以给定索引的循环顺序获得最佳性能。

5 个答案:

答案 0 :(得分:2)

您可以使用简单的for循环。在每次迭代中,您将获得索引i处的值,然后递增索引以获取下一个值。

你需要一个循环来迭代正确的次数和模运算符来从values列表中获取每个值:

private static void populateList(int i){
    children.clear();
    for(int p = 0; p < values.size(); p++){
        children.add(values.get(i++%values.size()));
    }
}

<小时/> 或者,您可以使用children列表的值填充values列表。然后只需调用Collections.rotate(注意列表中的索引是0基索引):

private void populateList(int i){
   Collections.rotate(children, -i);
}

测试片段:

public class Test { 
    private final static ArrayList<Integer> values = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8));
    private final static ArrayList<Integer> children = new ArrayList<>();

    public static void main (String[] args){
        populateList(2); //shift the elements in the list
        System.out.println(children);
        populateListUsingRotate(-2); //get back the original one
        System.out.println(children);
    }

    private static void populateList(int i){
        children.clear();
        for(int p = 0; p < values.size(); p++){
            children.add(values.get(i++%values.size()));
        }
    }

    private static void populateListUsingRotate(int i){
        Collections.rotate(children, -i);
    }
}

输出:

[3, 4, 5, 6, 7, 8, 1, 2]
[1, 2, 3, 4, 5, 6, 7, 8]

答案 1 :(得分:0)

使用ArrayList的subList方法

List list1 = values.subList(indexNum, values.size());
List list2 = values.subList(0, indexNum - 1);
children.clear();
children.addAll(list1);
children.addAll(list2);

答案 2 :(得分:0)

试一试:List subList(int startIndex, int endIndex)

import java.util.ArrayList;
import java.util.List;

class GetSubListOfJavaArrayList {

public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("element_1");
arrayList.add("element_2");
arrayList.add("element_3");
arrayList.add("element_4");
arrayList.add("element_5");
List subList = arrayList.subList(1,3);
System.out.println("Elements in sub list :");
for(int i=0; i < subList.size() ; i++)
System.out.println(subList.get(i));
Object obj = subList.remove(0);
System.out.println(obj + " is removed from sub list");
System.out.println("Elements in ArrayList :");
for(int i=0; i < arrayList.size() ; i++)
System.out.println(arrayList.get(i));

}
}

答案 3 :(得分:0)

如果你的问题是:

调整列表,使第一个元素位于圆圈中的位置索引。

@Test
public void canShiftTheListToTheSpecifiedIndexCircularly(){
    List<Integer> list = Lists.newArrayList();

    for(int i = 0; i < 8; i++){
        list.add(i + 1);
    }

    shiftList(3, list);

    assertTrue(3 == list.get(0));
    assertTrue(2 == list.get(list.size() - 1));
    assertTrue(8 == list.get(5));

    shiftList(-3, list);

    assertTrue(1 == list.get(0));
    assertTrue(8 == list.get(7));
}

private List<Integer> shiftList(int posision, List<Integer> list){
    //example above seems to use 1 based indexes, hence the oddities with the 1 magic number
    // --- 

    int indexOffset = 1;

    boolean isBackwards = posision < 0;
    int start = isBackwards ? list.size() + posision : 0;
    int end = isBackwards ? list.size() : posision - indexOffset;

    List<Integer> tempStorage = Lists.newArrayList();
    List<Integer> elmsToShift = list.subList(start, end);
    tempStorage.addAll(elmsToShift);
    elmsToShift.clear();
    if(isBackwards){
        list.addAll(0, tempStorage);
    }else{
        list.addAll(tempStorage);
    }
    return list;
}

答案 4 :(得分:0)

这可能是许多解决方案之一:

 private void populateList(int i)
{
    children.clear();
    List<Integer> temp = new ArrayList<Integer>(values.subList(0, i));

    for(int j=i; j<values.size(); j++)
        children.add(values.get(j));

    for(int l=0; l<temp.size(); l++)
        children.add(values.get(l));
}