Splitting a dynamic array into multiple arrays

I am trying to split a dynamic array of unknown length into multiple parts and return all the parts as a list of arrays, each array in a list of a given length. I searched here for a solution, but only found solutions for fixed length arrays. I had bash and came 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 go out of bounds all the time, but I'm not sure where this is wrong. Any help would be greatly appreciated, thanks!

EDIT :: For anyone looking and looking for a quick answer, this method splits up your standard arrays and adds the remainders to 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;
}

      

+3


source to share


1 answer


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

      

How can you copy something to an array null

?

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 when OP changed the code:

Your problem is here,



int place2 = splitSize;

      

You assign place2

with splitSize

and then create new string arrays like

String[] part = new String[splitSize];

      

Then you try to copy input

in part

to the index splitSize

. Remember that arrays are null indexed.

Let's say splitSize = 2

. Then part

will be an array of lengths of 2

indexed strings 0

and 1

.

Then on the line, System.arraycopy(input, place, part, place2, splitSize);

you try to copy something into the index part

2

. Hence, an event happened ArrayIndexOutOfBoundsException

.

Maybe this will 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);
    }
}

      

+2


source







All Articles