EGHDK
EGHDK

Reputation: 18130

Best way to split up an array in java to smaller arrays?

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

}

Upvotes: 3

Views: 2070

Answers (3)

hasan
hasan

Reputation: 24205

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){
}

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

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.

Upvotes: 2

Eng.Fouad
Eng.Fouad

Reputation: 117587

You can use Arrays.copyOfRange():

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

Upvotes: 4

Related Questions