Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a dynamic array into multiple arrays

I'm trying to split a dynamic array of unknown length into multiple parts and return all parts as an array list, each array in the list of given length. I've searched around on here for a solution but have only come across solutions for fixed length arrays. I've had a bash at it and come up with this:

private static ArrayList<String[]> list = new ArrayList<String[]>();

public static ArrayList<String[]> split(String input[], int splitSize){
    int place = 0;
    int place2 = splitSize;

    for(int i = 0; i < input.length/splitSize; i++){

        String[] part = new String[splitSize];
        System.arraycopy(input, place, part, place2, splitSize);

        place = place + splitSize;
        place2 = place2 + splitSize;

        System.arraycopy(input, place, part, place2, splitSize);
        list.add(part);

    }
    return list;
}

I keep getting out of bounds errors, but I'm not sure where it's wrong. Any help would be greatly appreciate, thanks!

EDIT:: For those googling and looking for a quick answer, this method splits your standard arrays and adds leftovers onto the end:

public static String[][] split(String[] input, int splitSize) {
    int numOfSplits = (int)Math.ceil((double)input.length / splitSize);
    String[][] output = new String[numOfSplits][];
    for(int i = 0; i < numOfSplits; ++i) {
        int start = i * splitSize;
        int length = Math.min(input.length - start, splitSize);
        String[] part = new String[length];
        System.arraycopy(input, start, part, 0, length);
        output[i] = part;
    }
    return output;
}
like image 466
Defa1t Avatar asked Mar 20 '26 11:03

Defa1t


1 Answers

String[] leftover = null;
System.arraycopy(input, place, leftover, place2, splitSize);

How can you copy something in a null array?

See the javadoc,

public static void arraycopy(Object src,
         int srcPos,
         Object dest,
         int destPos,
         int length)

Throws:

NullPointerException - if either src or dest is null.

EDIT as OP changed the code:

Your problem is here,

int place2 = splitSize;

You are assigning place2 with splitSize and then creating a new arrays of string like,

String[] part = new String[splitSize];

Then, you are trying to copy input into part in index splitSize. Remember, arrays are zero indexed.

Say, splitSize = 2. Then part will be array of length 2 of strings indexed 0 and 1.

Then in line System.arraycopy(input, place, part, place2, splitSize); you are trying to copy something in part's 2 index. Hence ArrayIndexOutOfBoundsException occurred.

May be this help you:

import java.util.ArrayList;

public class SplitArray {
    private static ArrayList<String> list = new ArrayList<String>();

    public static ArrayList<String> split(String input, int splitSize) {
        int place = 0;

        for (int i = 0; i < input.length() / splitSize; i++) {
            String part = input.substring(place, place + splitSize);

            list.add(part);

            place += splitSize;
        }
        return list;
    }

    public static void main(String[] args) {
        String input = "aabbcc";
        split(input, 2);
    }
}
like image 92
rakeb.mazharul Avatar answered Mar 22 '26 00:03

rakeb.mazharul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!