繁体   English   中英

方法根本不返回任何东西

[英]Method does not return anything at all

我编写了一个方法,该方法获取一个数字数组和一个整数(将作为定界符)作为输入,该方法将返回一个二维数组,该数组根据不包括定界符的定界符进行切片。 例子:

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]]

由于某种原因,当我将鼠标移到方法的名称上时,出现错误,我的函数应该返回int[][]

这是我的代码:

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

这是因为result是在for循环作用域中定义的。

请格式化代码,您将在return语句中看到result不是“可见”。

更新:友好的余数:您可以使用快捷键在Eclipse中格式化所选代码: Ctrl + Shift + F

格式化代码:

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

您可能会看到没有关闭方法} ,并且结果在for-loop作用域中定义,并从那里返回,但是我相信您要在for循环 之后返回result是'是吗?

我想您的代码应如下所示:

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

另外,我猜你在result[j]=Arrays.copyOfRange(input, firstindex, lastindex);result[j]=Arrays.copyOfRange(input, firstindex, lastindex);有一个错误result[j]=Arrays.copyOfRange(input, firstindex, lastindex); j可能会大于该countresult的大小)。 因此,您应该有另一个计数器或指针指向result数组中的最后一个空闲元素(复制数组的下一个块的目标)。

更新3:

好了,我不知道它是多么公平的,但我自己做所有工作。 这是实际起作用的代码(带有注释):

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

我使用接下来的几行来运行它,并且效果与预期的一样:

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

另外,在某些时候,您可能有空的零件(拆分后)。 因此,我留给您清理它(如果input数组中有两个连续的分割数,它们可能会出现在所有地方(不仅在开头或结尾处)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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