在java方法中将数组拆分为较小数组的最佳方法是什么?我希望能够将任何大小的数组都放入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);
}

编辑:

因此,将阵列复制到另一个阵列似乎效率不高。这样的事情会起作用吗?
    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;
    }

}

最佳答案

您可以使用 Arrays.copyOfRange() :

int from = 0;
int to = 4;
String[] subArray = Arrays.copyOfRange(receipts, from, to)

08-04 07:18