I have seen this question asked a few times, but I do not entirely understand the solutions provided. Below is a bit of code I came up with for splitting up an arraylist into five parts. I have tested and changed the array size from 0 to 16 and it works fine.
I am sure there is a "better" way of doing this, so I would like to see what others think.
public static void main(String[] args)
{
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("A");//1
arrayList.add("B");//2
arrayList.add("C");//3
arrayList.add("D");//4
arrayList.add("E");//5
arrayList.add("F");//6
arrayList.add("G");//7
arrayList.add("H");//8
arrayList.add("I");//9
arrayList.add("J");//10
arrayList.add("K");//11
arrayList.add("L");//12
arrayList.add("M");//13
arrayList.add("N");//14
arrayList.add("O");//15
arrayList.add("P");//16
arrayList.add("X");//17
int i1 = (int) Math.ceil(arrayList.size()/5.0);
List<String> sublist = new ArrayList<String>();
int x=0;
for(int p=0; p<i1; p++)
{
if(arrayList.size()>=(x+5))
{
sublist = new ArrayList<String>(arrayList.subList(x, x+5));
x+=5;
}
else
sublist = new ArrayList<String>(arrayList.subList(x, arrayList.size()));
for(String slist : sublist)
{
System.out.println("i:"+slist);
}
}
}