What is the best way to split up an array in a java method to smaller arrays? I want to be able to throw in any size array into takeReceipts(String[])
//Can handle any size array
public void takeReceipts(String[] receipts){
//split array into smaller arrays, and then call handleReceipts(String[]) for every smaller array
}
//This method can only handle arrays with the size of 5 or less
private void handleReceipts(String[] receipts){
myNetworkRequest(receipts);
}
EDIT:
So it seems like copying the array into another array isn't efficient. Would something like this work?
public void takeReceipts(String[] receipts){
int limit = 5;
int numOfSmallerArrays = (receipts.length/limit)+(receipts.length%limit);
int from = 0;
int to = 4;
for (int i = 0; i < numOfSmallerArrays; i++){
List<String> subList = Arrays.asList(receipts).subList(from, to);
from =+ limit;
to =+ limit;
}
}
numOfSmallerArrays
is off - it should beint numOfSmallerArrays = ((receipts.length+limit-1)/limit);
You also need to add a checkif (to >= receipts.length) to = receipts.length()-1;
int numOfSmallerArrays = ((receipts.length+limit-1)/limit);
better thanint numOfSmallerArrays = (receipts.length/limit)+(receipts.length%limit);
?