java - Method does not return anything at all -


i wrote method gets input array of numbers , integer (which delimiter) method return 2d array sliced according delimiter not including 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 reason when move mouse on name of method error function should return int[][]

this code:

public static int[][] splitarraybynum(int[] input, int number){         if (input[0]==number)                ( int = 0 ; < input.length - 1 ; i++ )                {                   input[ ] = input[ + 1 ] ;                 }          if ((input[(input.length)-1])==number)                ( int = (input.length)-1 ; < input.length - 1 ; i++ )                {                   input[ ] = input[ + 1 ] ;                 }                    int count = 0;                   (int = 0; < input.length; i++) {                     if (input[i] == number) {                       count++;                     }           int[][] result = new int[count][];         int firstindex=0;         int lastindex=0;           (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 because result defined in for-loop scope.

please, format code, , see result isn't "visible" return statement.

update: friendly remainder: can format selected code in eclipse shortkey: ctrl + shift + f

formatted code:

public static int[][] splitarraybynum(int[] input, int number){   if (input[0]==number)     ( int = 0 ; < input.length - 1 ; i++ )     {       input[ ] = input[ + 1 ] ;      }    if ((input[(input.length)-1])==number)     ( int = (input.length)-1 ; < input.length - 1 ; i++ )     {       input[ ] = input[ + 1 ] ;      }    int count = 0;   (int = 0; < input.length; i++) {     if (input[i] == number) {       count++;     }      int[][] result = new int[count][];     int firstindex=0;     int lastindex=0;      (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 don't have method closing }, , result defined in for-loop scope, , returned there, believe want return result after for-loop, isn't it?

i suppose code should this:

public static int[][] splitarraybynum(int[] input, int number){   if (input[0]==number)     ( int = 0 ; < input.length - 1 ; i++ )     {       input[ ] = input[ + 1 ] ;      }    if ((input[(input.length)-1])==number)     ( int = (input.length)-1 ; < input.length - 1 ; i++ )     {       input[ ] = input[ + 1 ] ;      }    int count = 0;   (int = 0; < input.length; i++) /*removed bracket here*/     if (input[i] == number) {       count++;     }    int[][] result = new int[count][];   int firstindex=0;   int lastindex=0;    (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, guess have mistake in line result[j]=arrays.copyofrange(input, firstindex, lastindex);. it's possible j larger count (the size of result). so, should have counter or pointer last free element in result array (destination copy next chunk of array).

update 3:

well, don't know how fair is, did your work myself. here code, works (with comments):

public static int[][] splitarraybynum(int[] input, int number) {     if(input.length == 0) {         return new int[0][];     }      int count = 0;     (int = 0; < input.length; i++) {         if (input[i] == number) {             count++;         }     }      if(input[input.length - 1] != number) {         /*we need add end of array manually*/         count ++;     }      int[][] result = new int[count][];     int firstindex = 0;      int iter = 0;     (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 end of array*/         result[count-1] = arrays.copyofrange(input, firstindex, input.length);     }      return result; } 

i run using next few lines, , works 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, @ point, may have empty parts (after split). so, leave clean (they may appear everywhere(not @ beginning or end), if there 2 consecutive split-numbers in input array).


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -