简体   繁体   中英

Method does not return anything at all

I wrote a method which gets as an input an array of numbers and an integer (which will be the delimiter) the method will return a 2D array that is sliced according to the delimiter not including the delimiter. Examples:

splitArrayNyNum([0, 0, 0, 3, 1, 1, 1], 3) -> [[0, 0, 0], [1, 1, 1]]

splitArrayNyNum([1, 2, 3, 1, 2, 3, 1, 1, 2, 2, 3, 1], 3) -> [[1, 2], [1, 2], [1, 1, 2, 2], [1]]

splitArrayNyNum([3 , 1 ,3 ,3], 3) -> [[1]]

For some reason when I move my mouse over the name of the method I get the error that my function should return int[][]

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

This is because result is defined in the for-loop scope.

Please, format your code, and you will see that result isn't "visible" from return statement.

UPDATE: Friendly remainder: you can format selected code in eclipse with a shortkey: Ctrl + Shift + F

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

You may see that you don't have the method closing } , and the result is defined in the for-loop scope, and is returned from there, but I believe that you want to return the result after the for-loop , isn't it?

I suppose that your code should look like this:

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++) /*removed bracket here*/
    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 ;
}

also, I guess that you have a mistake in the line result[j]=Arrays.copyOfRange(input, firstindex, lastindex); . It's possible that j will be larger that count (the size of the result ). So, you should have another counter or pointer to the last free element in the result array (destination to copy the next chunk of array).

UPDATE 3:

Well, I don't know how much fair it is, but I did all YOUR work by myself. Here is the code, which actually works (with comments):

public static int[][] splitArrayByNum(int[] input, int number) {
    if(input.length == 0) {
        return new int[0][];
    }

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

    if(input[input.length - 1] != number) {
        /*we need to add the end of the array manually*/
        count ++;
    }

    int[][] result = new int[count][];
    int firstIndex = 0;

    int iter = 0;
    for (int j = 0; j < input.length; j++) {
        if (input[j] == number) {
            result[iter++] = Arrays.copyOfRange(input, firstIndex, j);
            firstIndex = j+1;
        }
    }
    if(input[input.length - 1] != number) {
        /*manually adding the end of the array*/
        result[count-1] = Arrays.copyOfRange(input, firstIndex, input.length);
    }

    return result;
}

I run it using the next few lines, and that works just like expected:

int[] inp = new int[]{1, 3, 3, 5};
int[][] sp = splitArrayByNum(inp, 3);
for(int i=0;i<sp.length;i++) {
    System.out.print(i + ": ");
    for(int j=0;j<sp[i].length;j++) {
        System.out.print(sp[i][j] + " ");
    }
    System.out.println();
}

Also, at some point, you may have empty parts (after the split). So, I leave it up to you to clean it up (they may appear everywhere(not only at the beginning or the end), if there are two consecutive split-numbers in the input array).

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