Navaneeth Sen
Navaneeth Sen

Reputation: 6466

Adding from ArrayList to another ArrayList from a given index

I have two arraylist

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

I have a method which when called with a value(index number) should fill the children arrayList taking values from the values ArrayList starting at the given index i and filling it circularly.

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));
    ...
}

I need a logic that will copy the values from the values arrayList to children arrayList with best performance in a circular order from the given index.

Upvotes: 0

Views: 1294

Answers (5)

AnthonyJClink
AnthonyJClink

Reputation: 1008

If your question is:

Adjust list so the first element at position index in a circle.

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

Upvotes: 0

Arjit
Arjit

Reputation: 3456

Use subList method of ArrayList

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

Upvotes: 0

Shekhar Khairnar
Shekhar Khairnar

Reputation: 2691

This could be one of many solution :

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

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93842

You can use a simple for loop. At each iteration you get the value at index i, then increment the index to get the next value.

You need a loop to iterate the correct number of times and the modulo operator to get each value from the values list:

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


Or you can fill the children list with the values of the values list. Then just call Collections.rotate (note that indexes in a list are 0 base indexed):

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

Snippet of test:

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

Output:

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

Upvotes: 2

Benjamin
Benjamin

Reputation: 2286

Try This :The 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));

}
}

Upvotes: 0

Related Questions