3

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;
    }

}
11
  • Check out my answer. it handles it. Commented Nov 18, 2014 at 18:13
  • Your computation of numOfSmallerArrays is off - it should be int numOfSmallerArrays = ((receipts.length+limit-1)/limit); You also need to add a check if (to >= receipts.length) to = receipts.length()-1; Commented Nov 18, 2014 at 18:13
  • My solution handles that with Math.min(i+5, receipts.length-1) Commented Nov 18, 2014 at 18:17
  • First, @dasblinkenlight why is int numOfSmallerArrays = ((receipts.length+limit-1)/limit); better than int numOfSmallerArrays = (receipts.length/limit)+(receipts.length%limit);? Commented Nov 18, 2014 at 18:19
  • Second, @dasblinkenlight where do I want to add that if statement? Commented Nov 18, 2014 at 18:20

3 Answers 3

4

You can use Arrays.copyOfRange():

int from = 0;
int to = 4;
String[] subArray = Arrays.copyOfRange(receipts, from, to)
Sign up to request clarification or add additional context in comments.

5 Comments

Isn't System.arraycopy faster?
Arrays.copyOfRange() uses System.arraycopy() internally.
I'm doing: Arrays.copyOfRange(receipts, from, to) from = 0 and to = 2, but the array I get back is only receipts[0] and receipts[1]. Why?
@EGHDK did you read the docs he linked? "to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)"
So from is inclusive and to is exclusive?
2

If you are open to using List<String> in place of String[] arrays, you could do partitioning in a very economical way:

List<String> subList = Arrays.asList(receipts).subList(from, to);

This approach does not make a copy of your array, providing a read-only view into the original array of receipts.

static final int LIMIT = 10;

public static void process(List<String> small) {
    if (small.size() > LIMIT) {
        System.out.print("Array is too big: "+small.size());
        return;
    }
    for (String s : small) {
        System.out.print(s+" ");
    }
    System.out.println();
}

public static void processBig(String[] receipts) {
    int numChunks = ((receipts.length+LIMIT-1)/LIMIT);
    int from = 0;
    int to = LIMIT;
    List<String> bigList = Arrays.asList(receipts);
    for (int i = 0 ; i != numChunks ; i++) {
        List<String> subList = bigList.subList(from, to);
        process(subList);
        from += LIMIT;
        to += LIMIT;
        if (to >= receipts.length) {
            to = receipts.length;
        }
    }
}

Demo.

The consequences of taking this approach are that the changes made to the original array elements become "visible" through the view, and that you cannot change the resultant subList in any way.

1 Comment

How would I implement this in the takeReceipts(String[] receipts) method? Check my update. Thanks
1
public void takeReceipts(String[] receipts){
    for (int i=0; i< receipts.length; i+=5)
        handleReceipts(Arrays.copyOfRange(receipts, i, Math.min(i+4, receipts.length-1)));
}

private void handleReceipts(String[] receipts){ 
}

OR

public void takeReceipts(String[] receipts){
    for (int i=0; i< receipts.length; i+=5)
        handleReceipts(Arrays.asList(receipts).subList(i, Math.min(i+4, receipts.length-1)));
}

private void handleReceipts(List<String> receipts){
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.