【问题标题】:Array compared to a variable method call数组与变量方法调用的比较
【发布时间】:2017-08-30 09:41:47
【问题描述】:

这里是初学者,我目前正在编写一个程序,它接受一个数组和一个变量并比较两者,如果声明的变量在数组中,则数组将打印出数组中除了包含的数字之外的所有数字在变量中。这是正常工作和运作。此外,我现在想做的是将我编写的代码放入一个可重复使用的功能方法中,但我目前有点坚持这样做。最终结果将是显示一个不再包含数字 17 的新的较小数组。 这是我的代码示例。

public class arrayTest
{
   public static void main(String [] args)
   {
      int[]arrayA = {2,4,8,19,32,17,17,18,25,17};

      int varB = 17;

      for(int B = 0; B < arrayA.length; B++)
      {
         if(arrayA[B] != varB)
         {
            System.out.print(arrayA[B] + " ");
         }

      }
   }

      public static int newSmallerArray( int[] A, int b )
      {

      }
}

【问题讨论】:

  • 为什么返回类型是 int?它不应该像在 main 方法中那样打印它吗?换句话说,返回类型为 void 或返回数组是合适的。
  • 您要返回较小的数组,还是只打印它?此外,您的 IDE 有一个“提取方法”的重构选项,可能
  • 请参考这篇文章 [link] (stackoverflow.com/questions/5098540/delete-element-from-array) 它和你的问题一样。
  • Clark 有没有回答您的问题?

标签: java arrays for-loop if-statement methods


【解决方案1】:

好的,到此为止

  int[]arrayA = {2,4,8,19,32,17,17,18,25,17};
  int varB = 17;

你可以打电话

 newSmallerArray(arrayA, varB);

然后,在这种方法中,您将拥有相同的循环代码,但变量名称已更改

  for(int B = 0; B < A.length; B++)
  {
     if(A[B] != b)
     {
        System.out.print(A[B] + " ");
     }
  }

现在的问题是,你已经声明这个方法返回一个int,但是没有什么可以返回,所以也许让它返回 void

public static void newSmallerArray( int[] A, int b )

接下来,我也会坚持 java 命名约定

返回输入数组子集的完整代码示例

使用更好的名字

public static void main(String[] args) throws SocketException {

    int[]arrayA = {2,4,8,19,32,17,17,18,25,17};
    int varB = 17;

    int [] na = newSmallerArray(arrayA, varB);
    // test
    System.out.println();
    for(int b = 0; na != null && b < na.length; b++)
    {
        System.out.print(na[b] + " ");
    }        
}

private static int[] newSmallerArray(int[] a, int b) {

    int count = 0;
    for(int x = 0; x < a.length; x++)
    {
        if(a[x] != b)
        {
            System.out.print(a[x] + " ");
            count++;
        }
    }

    if (count > 0) {
        int[] newArray = new int [count];
        count = 0;
        for(int x = 0; x < a.length; x++)
        {
            if(a[x] != b)
            {
                newArray[count++] = a[x];
            }
        }           

        return newArray;
    }

    return null;

}

【讨论】:

  • 我希望它返回数组中没有 17 的数组
  • @Clark1776 你需要用你刚刚写的评论更新你的帖子。
  • 看起来不错,但对于我要实现的程序来说非常复杂
  • 我的原始答案不包括这个我希望它返回数组中没有 17 的数组,但这就是你所要求的。你接受的答案不包括这个。
【解决方案2】:

我希望它返回数组中没有 17 的数组

然后,假设您使用的是 Java 8+,请使用 IntStreamfilter 输出 b 值。喜欢,

public static int[] newSmallerArray(int[] arr, int b) {
    return IntStream.of(arr).filter(x -> x != b).toArray();
}

如果您想在没有 Java 8 功能的情况下执行此操作,您可以计算要排除的值的出现次数。喜欢,

private static int countValue(int[] arr, int b) {
    int count = 0;
    for (int value : arr) {
        if (value == b) {
            count++;
        }
    }
    return count;
}

并使用它来正确调整新数组的大小,然后复制值。喜欢,

public static int[] newSmallerArray(int[] arr, int b) {
    int[] ret = new int[arr.length - countValue(arr, b)];
    for (int i = 0, p = 0; i < arr.length; i++) {
        if (arr[i] != b) {
            ret[p++] = arr[i];
        }
    }
    return ret;
}

【讨论】:

    【解决方案3】:

    只需重构包含主要逻辑的代码并将其移至方法。使用IDE很容易做到。否则,您必须将代码复制到新方法并传递必要的参数。

    public class arrayTest
    {
       public static void main(String [] args)
       {
          int[] arrayA = {2,4,8,19,32,17,17,18,25,17};
          int varB = 17;
          // Call with the array and variable you need to find.
          newSmallerArray(arrayA, varB);
       }
    
       public static void newSmallerArray( int[] arrayA, int varB)
       {
          for(int B = 0; B < arrayA.length; B++)
          {
             if(arrayA[B] != varB)
             {
                System.out.print(arrayA[B] + " ");
             }
          }
       }
    }
    

    【讨论】:

    • 正如@scary-wombat 所建议的,您需要遵循 java 命名约定。
    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多