I have list of String objects defined as List<String> and I want to split this List into sublist such that each sublist should be of length <=6 and number of chars in each sublist should not exceed 240 chars.
Special condition : If the total no. of chars in the sublist exceeds 240 chars then we have to decrease the no. of elements in the sublist such that it meets the length of 240 and shift the rest elements in next sublist and so on.
My recommendation would be to reduce those numbers in the first sentence so to come up with the solution would be considerably easier. From the requirement (first sentence) I'm assuming there might be N levels of nesting, correct?
Khuzema Dharwala wrote:If the total no. of chars in the sublist exceeds 240 chars then we have to decrease the no. of elements in the sublist such that it meets the length of 240 and shift the rest elements in next sublist and so on.
Seems like you already have a particular implementation in mind? I wouldn't do it that way. I would go through the list and consider each String. If it's going to make the existing sublist longer than 6 or bigger in total than 240 characters, I would create a new empty sublist before proceeding with that String.
(That would not work correctly if one of the Strings in the original list was longer than 240 characters, it might even cause an infinite loop depending on the implementation. But that's all right because the requirements don't say what should happen in that case.)
After writing this, I realize I may have misinterpreted it. My interpretation was that each of the split Lists need to be of the same size (the size <= 6). If that's not true, then not all of my answer will apply.
My approach here would be to first discover the sublist length before you begin splitting it into sublists. You really don't want to get halfway through and then find out your sublists need to be shorter than you originally thought.
Here's what I'd do. First write a method to calculate the total characters from index a to b of a List of Strings:
After that, write a method to calculate the sublist size required. You'll probably need a nested for loop of some kind, basically you're going to try starting with 6, see if the rules are violated. If not, you return 6. If so, you decrease by 1 and try again.
After you find the sublist size, write a method to break a list into multiple lists.
Your approach here will probably be to use another for loop going to the sublist size. Each iteration of the for loop will create a new List<String> and add it to the List<List<String>>. At the end, each inner list of string will be of the desired size.
Ok, re-read once again. Yesterday I think I slightly misunderstood the text.
So really what you need to do is, let's assume a starting list contains a singular element of 480 chars, so you need to divide it in chunks of 40 chars (12 in total) and create two sublists with 6 chunks in each, right?
So my question is, what you can and what you can't do when a starting list contains 2 or more elements. Do those elements can be combined? Asking this, because that depends whether you efficiently would use those element spaces in each sublist.
So far I count three different interpretations of the question. (You may have heard the story about the seven blind men and the elephant.) Therefore I think it would help if we got a more complete description of the requirements. Khuzema? More info? Or have you found the right answer while the posters here are fumbling around?