繁体   English   中英

数组与变量方法调用的比较

[英]Array compared to a variable method call

在这里的初学者程序员,我目前编写了一个程序,该程序接受一个数组和一个变量,然后将两者进行比较,如果声明的变量在数组中,则该数组将打印出数组中除变量中包含的数字以外的所有数字。 。 这是正常工作,并且功能正常。 此外,我现在想做的是将我编写的代码放入一种可以重复使用的有效方法中,但是目前我仍然坚持这样做。 最终结果将是显示一个新的较小数组,该数组不再包含数字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[]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

然后,假设您使用的是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;
}

只需重构包含主要逻辑的代码,然后将其移至方法即可。 使用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] + " ");
         }
      }
   }
}

暂无
暂无

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

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