Writing a program to read a maximum of 99 elements in an iteration from a list I want to read a list of elements and pass a maximum of 99 elements to a function for some logical operations.

I have tried this with an array as an example and this code was successful in achieving my purpose.

I just want someone to review it and help optimize it.

/** 
 */
package com.review.code.java;

/**
 * i&gt;   Read a List Input,
 * ii&gt;  Call a Function with maximum of 99 elements in one iteration Eg:(0..98; 99..198; 199..297; .....)
 * 
 *
 */
public class OptimizeReadingList {

    public static int list_size=107;//252,543,... etc. - Input List Size

    public static int a[] = new int[list_size];

    public static void main(String[] args) {

        int read_max_size = 99;
        int input_list_size = list_size;

        // Add Elements to the Array
        for(int j =0;j&lt;input_list_size;j++){
            a[j]=j+1;
        }

        // Print all the Elements in the List 
        for(int j =0;j&lt;input_list_size;j++){
            System.out.print(a[j]+",");
        }

        System.out.println("\n Print only a max of 99 elements in one iteration using the printElements method");

        // Should Call a function printElements with start_index and end_index
        for(int i = 0,ele = (read_max_size-1); i&lt;input_list_size ; ){

            printElements(i,ele);

            // Increment Operations for the next elements(maybe another 99 or less than 99)
            i=(ele+1);
            if((ele+read_max_size &gt; input_list_size)){
                ele=input_list_size;
            }else{
                ele=ele+ele;
            }
        }

    }

    // Consider that this method can read only a maximum of 99 elements in range.
    private static void printElements(int i, int ele) {
        for(int j = i;j&lt;ele;j++){
            System.out.print(a[j]+",");
        }
        System.out.println("\n");
    }

}
First things first, I feel the biggest problem about your code is that your comments lie.

/**
 * i&gt;   Read a List Input,
 * ii&gt;  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&lt;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&lt;E&gt; 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 &lt;T&gt; void splitProcess(List&lt;T&gt; list) {
    splitProcess(list, BATCH_SIZE);
}

private static &lt;T&gt; void splitProcess(List&lt;T&gt; list, int batchSize) {
    if (batchSize &lt; 1) {
        throw new IllegalArgumentException("Batch size must not be less than 1.");
    }
    for (int start = 0, end; start &lt; (end = Math.min(list.size(), start + batchSize));
            start = end) {
        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 &lt; 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...