I have a List that I need to split because it gets sent to Google translate via the API and it has a List element size limit of 1024.
Here's the code (including the debugging code I added to try and figure this out):
int size = MASTER_LINE_LIST.size();
int third = size / 3;
int start1 = 0;
int end1 = start1 third;
int start2 = end1 1;
int end2 = start2 third;
int start3 = end2 1;
int end3 = size - 1;
String msg1 = "start1 = " start1 "\n"
"end1 = " end1 "\n\n"
"start2 = " start2 "\n"
"end2 = " end2 "\n\n"
"start3 = " start3 "\n"
"end3 = " end3 "\n\n";
List<String> lineListSub0 = MASTER_LINE_LIST.subList(start1, end1);
List<String> lineListSub1 = MASTER_LINE_LIST.subList(start2, end2);
List<String> lineListSub2 = MASTER_LINE_LIST.subList(start3, end3);
int mllSize = MASTER_LINE_LIST.size();
int ll0Size = lineListSub0.size();
int ll1Size = lineListSub1.size();
int ll2Size = lineListSub2.size();
int llTotal = ll0Size ll1Size ll2Size;
String msg2 = "Master list size = " mllSize "\n\n"
"sublist 1 size = " ll0Size "\n"
"sublist 2 size = " ll1Size "\n"
"sublist 3 size = " ll2Size "\n\n"
"total size of sublists = " llTotal;
System.out.println(msg1);
System.out.println(msg2);
Here is the output from this code:
start1 = 0
end1 = 949
start2 = 950
end2 = 1899
start3 = 1900
end3 = 2846
Master list size = 2847
sublist 1 size = 949
sublist 2 size = 949
sublist 3 size = 946
total size of sublists = 2844
As you can see, the original List has 2,847 elements, yet the sum of the sizes of the subLists adds up to 2,844 ... I somehow lost three elements and I can't seem to find them because when I look at my start and end indexes that I use for the splits, they appear to me to be a cumulative series of numbers from 0 to 2846, as the start index of the next sublist is the end index of the previous sub-list plus 1 ... it looks right to me but why is it not adding up? Each sub-list SHOULD contain exactly 949 elements ... yet that third one doesn't ... but if I expand the boundaries of the indexes, I'll either get repeating data, or I'll go out of bounds.
What am I missing?
CodePudding user response:
Keep in mind that subList()
does not include the toIndex
, which means that in the following subList()
you need to start at the toIndex
of the previous subList()
.
int size = MASTER_LINE_LIST.size();
int third = size / 3;
int start1 = 0;
int end1 = start1 third;
int start2 = end1;
int end2 = start2 third;
int start3 = end2;
int end3 = size - 1;