简体   繁体   中英

Trouble splitting an array of integers using a delimiter

I'm writing a method which gets as an input an array of integers and an integer which is the delimiter. It splits the array according to the delimiter and it returns a 2D array so that each line contains the numbers in the array up until the delimiter (not including the delimiter).

Examples:

Input :

{1, 2, 3, 1, 2, 3, 1, 1, 2, 2, 3, 1}, 3

Output:

[1, 2]
[1, 2]
[1, 1, 2, 2]
[1]

Input: {3, 1, 3, 3}, 3

Output: [1]

but instead when I run my code I don't get anything in return, I ran it in debug mode and I got that my result was [null, null, [1, 2]]

What should I fix in my code?

public static int[][] splitArrayByNum(int[] input, int number) {
    if (input[0] == number)
        for (int i = 0; i < input.length - 1; i++) {
            input[i] = input[i + 1];
        }

    if ((input[(input.length) - 1]) == number)
        for (int i = (input.length) - 1; i < input.length - 1; i++) {
            input[i] = input[i + 1];
        }

    int count = 0;
    for (int i = 0; i < input.length; i++)
        if (input[i] == number) {
            count++;
        }

    int[][] result = new int[count][];
    int firstindex = 0;
    int lastindex = 0;

    for (int j = 0; j < input.length; j++) {

        if (input[j] == number) {
            result[j] = Arrays.copyOfRange(input, firstindex, lastindex);
            firstindex = lastindex = j;
        }
        lastindex++;
    }
    return result;
}

Well I've had a crack at it, can someone else come up with something a bit more elegant cus this doesn't feel like it's as simple as it could be?

public static int[][] arraySplitByDelimiter(int[] inputArray, int delimiter) {

           //Make an array to hold our results, ideally it would be nice to use an ArrayList
        //so that it can expand dynamically but instead we can also make one that we know will be big enough
        //if every other int is a delimiter then we can end up with a result array of inputArray.length / 2
        int[][] temporaryResultArray = new int[inputArray.length / 2][];
        int numberOfResultArrays = 0;

        int lastDelimterPosition = 0;

        for (int i = 0; i < inputArray.length; i++) {
            //If we find a delimeter copy the chunk of the input array to a new array in the temporaryResultArray
            if (inputArray[i] == delimiter) {
                temporaryResultArray[numberOfResultArrays++] = Arrays.copyOfRange(inputArray, lastDelimterPosition, i);
                lastDelimterPosition = i + 1;
            } else if (i == (inputArray.length - 1)) {
                //If we're at the end of the array then we should copy the last chunk to new array
                temporaryResultArray[numberOfResultArrays++] = Arrays.copyOfRange(inputArray, lastDelimterPosition, inputArray.length);
            }
        }

        int[][] finalResultArray = new int[numberOfResultArrays][];
        for (int i = 0; i < numberOfResultArrays; i++) {
            finalResultArray[i] = temporaryResultArray[i];
        }

        return finalResultArray;

    }

Just to illustrate flow logic:

int[][] result = new int[][];
int resultIndex = 0;

int[] chunk = new int[];
int chunkIndex = 0;

for (int i=0; i<input.length; i++) {
   if (input[i] != number) {
      chunk[chunkIndex++] = input[i];
   }
   else {
      if (chunk.length > 0) {
         result[resultIndex++] = chunk;
         chunk = new int[];
         chunkIndex = 0;
      }
   }
}
if (chunk.length > 0) {
   result[resultIndex] = chunk;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM