First things first, I feel the biggest problem about your code is that your comments lie.
/**
* i> Read a List Input,
* ii> Call a Function with maximum of 99 elements in one iteration Eg:(0..98; 99..198; 199..297; .....)
*
*
*/
public class OptimizeReadingList
There is no List
to be seen, and I am not sure what is meant by 'call a function'. Javadocs generally explain what the class/method is supposed to be used for, and they hardly talk about their underlying/reliant methods (except when the @see
literal is used). At the very most, sometimes Javadocs cover "implementation notes" or "API notes" highlighting the caveats of such an implementation, but that's all to it.
// Should Call a function printElements with start_index and end_index
for(int i = 0,ele = (read_max_size-1); i<input_list_size ; ){
Should it, should it not? Why so? Even for general Javadoc comments, it sounds weird to be suggesting something so specific... if you decide to rename your method down the road, you'll have to update this comment, which becomes cumbersome in the long run.
// Increment Operations for the next elements(maybe another 99 or less than 99)
i=(ele+1);
Let's start printing batches of 98 instead. You'll have to manually update refrences to 99
throughout your code. Comments should document the 'why' and not the 'how', hence your comments describing how you are selecting maybe the next 99, or less than 99, is usually not recommended.
// Consider that this method can read only a maximum of 99 elements in range.
private static void printElements(int i, int ele) {
I'm only highlighting the method declaration, but nowhere in the method says that a maximum of 99 elements will be read. If I call this as printElements(0, 0);
, I will only get a newline output. If I call this as printElements(9000, 10000);
, I will probably get an ArrayIndexOutOfBoundsException
exception (since you have hard-coded your test array as 107 elements).
So how can I actually achieve what I need?
You say you need a List
, so let's see if there are any methods that can help you out with...
List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex
, inclusive, and toIndex
, exclusive. (If fromIndex
and toIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.
private static final int BATCH_SIZE = 99;
private static <T> void splitProcess(List<T> list) {
splitProcess(list, BATCH_SIZE);
}
private static <T> void splitProcess(List<T> list, int batchSize) {
if (batchSize < 1) {
throw new IllegalArgumentException("Batch size must not be less than 1.");
}
for (int start = 0, end = Math.min(list.size(), start + batchSize); start < list.size();
start = end, end = Math.min(list.size(), start + batchSize)) {
System.out.println(list.subList(start, end));
}
}
- Define your desired batch size as
BATCH_SIZE = 99
.
- Have a 1-argument
splitProcess(List)
method that calls the 'fuller' splitProcess(List, int)
method with BATCH_SIZE
as the default second argument.
- Do some validation for
batchSize
.
- Construct a
for
-loop with:
- two variables representing the
start
(inclusive) and end
(exclusive) points
- looping while
start < list.size()
, and
- increment them accordingly.
Then you can call list.subList(start, end)
, and use it in the way you require.
Javadocs shall be left as an exercise for the reader...