patternjavaModerate
Writing a program to read a maximum of 99 elements in an iteration from a list
Viewed 0 times
maximumelementsreadwritingprogramiterationlistfrom
Problem
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.
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> 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 {
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 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<ele;j++){
System.out.print(a[j]+",");
}
System.out.println("\n");
}
}Solution
First things first, I feel the biggest problem about your code is that your comments lie.
There is no
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.
Let's start printing batches of 98 instead. You'll have to manually update refrences to
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
So how can I actually achieve what I need?
You say you need a
Returns a view of the portion of this list between the specified
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...
/**
* 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 OptimizeReadingListThere 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 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 void splitProcess(List list) {
splitProcess(list, BATCH_SIZE);
}
private static void splitProcess(List list, int batchSize) {
if (batchSize < 1) {
throw new IllegalArgumentException("Batch size must not be less than 1.");
}
for (int start = 0, end; start < (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 withBATCH_SIZEas the default second argument.
- Do some validation for
batchSize.
- Construct a
for-loop with:
- two variables representing the
start(inclusive) andend(exclusive) points
- looping while
start
- 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...
Code Snippets
/**
* 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// Should Call a function printElements with start_index and end_index
for(int i = 0,ele = (read_max_size-1); i<input_list_size ; ){// Increment Operations for the next elements(maybe another 99 or less than 99)
i=(ele+1);// Consider that this method can read only a maximum of 99 elements in range.
private static void printElements(int i, int ele) {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; start < (end = Math.min(list.size(), start + batchSize));
start = end) {
System.out.println(list.subList(start, end));
}
}Context
StackExchange Code Review Q#94883, answer score: 15
Revisions (0)
No revisions yet.