JAVA - subList returns empty lists inside FOR loop -
i'm facing following issue. have list total (size: s) , need split in n contiguous blocks. here code:
int start_index, end_index; for(int = 0; < n; i++){ start_index = (s/n)*i; end_index = start_index + (s/n) - 1; list<string> subblock = total.sublist(start_index, end_index-1); system.out.println("block "+i+": "+start_index+" "+end_index); system.out.println("block "+i+": "+subblock .tostring()); }
indexes correct: first system.out.println
works fine. subblock
empty. sublist call outside loop works fine: example, total.sublist(0,2)
returns first , second element expected.
is there i'm missing here?
i think made mistake in calculation of start , end index. try following code.
for(int = 0; < n; i++){ start_index = (s/n)*i; end_index = start_index + (s/n); list<string> subblock= (total.sublist(start_index, end_index)); system.out.println("block "+i+": "+start_index+" "+end_index); system.out.println("block "+i+": "+subblock .tostring()); }
Comments
Post a Comment