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.
    – hasan
    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)
    – hasan
    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);?
    – EGHDK
    Commented Nov 18, 2014 at 18:19
  • Second, @dasblinkenlight where do I want to add that if statement?
    – EGHDK
    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)
5
  • 1
    Isn't System.arraycopy faster?
    – royhowie
    Commented Nov 18, 2014 at 18:32
  • Arrays.copyOfRange() uses System.arraycopy() internally.
    – Eng.Fouad
    Commented Nov 18, 2014 at 19:11
  • 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
    Commented Nov 18, 2014 at 19:12
  • @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.)"
    – royhowie
    Commented Nov 18, 2014 at 19:14
  • So from is inclusive and to is exclusive?
    – EGHDK
    Commented Nov 18, 2014 at 19:26
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
  • How would I implement this in the takeReceipts(String[] receipts) method? Check my update. Thanks
    – EGHDK
    Commented Nov 18, 2014 at 18:06
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){
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.