簡體   English   中英

使用分隔符難以拆分整數數組

[英]Trouble splitting an array of integers using a delimiter

我正在寫一個方法,該方法將輸入一個整數數組和一個作為定界符的整數作為輸入。 它根據定界符分割數組,並返回一個2D數組,以便每一行都包含數組中的數字,直到定界符(不包括定界符)為止。

例子:

輸入:

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

輸出:

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

輸入: {3, 1, 3, 3}, 3

輸出: [1]

但是相反,當我運行代碼時,我沒有得到任何回報,而是在調試模式下運行了它,結果是[null, null, [1, 2]]

我應該在代碼中解決什么?

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

好吧,我對此有所了解,其他人可以提出一些更優雅的建議嗎,這似乎並不像它那么簡單嗎?

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;

    }

只是為了說明流程邏輯:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM