how to add a list of strings to another list at specific position? [closed]

Solution for how to add a list of strings to another list at specific position? [closed]
is Given Below:

I have two lists A,B. Now I want to add B elements in list A at index of 2,4,6,…

List<String> A = new ArrayList<>();
List<String> B = new ArrayList<>();

//A contains {"a","b","c","d","e","f","g"}
//B contains {"1","2","3","4"}

//Needed output is {"a","b","1","c","d","2","e","f","3","g"}

How can I achieve this in java

You can do it using List.add(index, value) method

Try following code:

public class Test {
    public static void main(String[] args) {
        List<String> A = new ArrayList<>();
        A.add("a");
        A.add("b");
        A.add("c");
        A.add("d");
        A.add("e");
        A.add("f");
        A.add("g");
        List<String> B = new ArrayList<>();
        B.add("1");
        B.add("2");
        B.add("3");
        B.add("4");

        int indToAdd = 2;
        for(int i = 0; i < B.size() && indToAdd < A.size(); i++) {
            A.add(indToAdd, B.get(i));
            indToAdd += 3;
        }
        System.out.println(A);
    }
}

Output of main() method call is:

[a, b, 1, c, d, 2, e, f, 3, g]