Praveenkumar Patrana Praveenkumar Patrana - 17 days ago 5
Java Question

Split an Arraylist into multiple variable length Arraylists

I have two ArrayLists. The first list contains some

x
number of elements these elements are again ArrayLists. The second list contains only integer values.

Second list size is always less than first list size Example of first list is: [[TC1,TC1_1],[,TC1_2],[TC2,TC2_1],[TC3,TC3_1][,TC3_2],[TC4,TC4_1][,TC4_2][,TC4_3],[TC5,TC5_1],[TC6,TC6_1]]

Example of second list is
[0,2,3,5,8,9]
.This second Araylist elements points to 1st arraylist where the 1st element of individual ArrayList elements of first arraylist is not empty

Now the requirement is 1st list should be split into 6 lists where 6 is 2nd list size. Output should be as below.

Of the 6 new lists 1st list would contain elements of main list from 0th to 1st index. The 2nd list would contain elements of main list from 3rd index. The 3rd list would contain elements of main list from 4th to 5th index and so on..finally output will be as below

`[[[TC1,TC1_1],[,TC1_2]]
[[TC2,TC2_1]]
[[TC3,TC3_1][,TC3_2]]
[[TC4,TC4_1][,TC4_2][,TC4_3]]
[[TC5,TC5_1]]
[[TC6,TC6_1]]]



private ArrayList getTCsIncludingSubTcs(ArrayList TCIndexes,ArrayList alTCs) {
//List tempSubList=new ArrayList();
ArrayList finalTCS=new ArrayList();

for(int j=0;j<TCIndexes.size();j++){
int TCIndex=(int)TCIndexes.get(j);
if(j==0 && TCIndexes.size()==1){

//List tempSubList=new ArrayList(alTCs.subList(TCIndex,(int)alTCs.size()));
List tempSubList=getSubArrayList(TCIndex,(int)alTCs.size(),alTCs);

finalTCS.add(tempSubList);
break;
}
int nextTCIndex=j+1;
System.out.println(nextTCIndex);
if(j==TCIndexes.size()-1){
List tempSubList=getSubArrayList(TCIndex,(int)alTCs.size(),alTCs);
finalTCS.add(tempSubList);
break;
}
List tempSubList=getSubArrayList(TCIndex,(int)alTCs.size(),alTCs);
finalTCS.add(tempSubList);

System.out.println("finalTCS :"+finalTCS);



}
System.out.println("fc size"+finalTCS.size());
return finalTCS;
}

Answer

Try this code.

public class Test {

private List<List<String>> getTcsIncludingSubTcs(ArrayList<Integer> tcIndexes,ArrayList<String> alTcs) {

    List<List<String>> finalTcs=new ArrayList<>();
    List<String> temp = new ArrayList<>();

    int size = tcIndexes.size();
    int i= alTcs.size();
    int k = 0;
    for(int j=0; j<alTcs.size(); j++){

        if(( j != 0 && j % size == 0 && i >= size))
        {
            temp = new ArrayList<>();               
            k++;    
        }
        i--;
        temp.add(alTcs.get(j));

        if(finalTcs.contains(temp))
            finalTcs.remove(k);
        finalTcs.add(k, temp);              
    }

            return finalTcs;
}

public static void main (String arg[])
{
    ArrayList<String> data1 = new ArrayList<>();
    data1.add("TC1");
    data1.add("TC2");
    data1.add("TC3");
    data1.add("TC4");
    data1.add("TC5");
    data1.add("TC6");
    data1.add("TC7");
    data1.add("TC8");
    data1.add("TC9");
    data1.add("TC10");

    ArrayList<Integer> data2 = new ArrayList<>();
    data2.add(0);
    data2.add(3);
    data2.add(6);

    Test t = new Test();    
    List<List<String>> list = t.getTcsIncludingSubTcs(data2, data1);
    for(List<String> l : list)
        System.out.println(l);

}   

}

Output:

[TC1, TC2, TC3]
[TC4, TC5, TC6]
[TC7, TC8, TC9, TC10]