Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用方法参数查找数组最小值的优雅方法?_Java_Arrays_Parameters_Java Stream_Min - Fatal编程技术网

Java 使用方法参数查找数组最小值的优雅方法?

Java 使用方法参数查找数组最小值的优雅方法?,java,arrays,parameters,java-stream,min,Java,Arrays,Parameters,Java Stream,Min,我正在寻找一种优雅的方式来表达这个伪代码。对于我的赋值,我无法更改方法签名或参数类型 private static int smallest(int... nums) { return Arrays.stream(nums).min().getAsInt(); } 我所要做的就是从方法调用中获取大量不同的int作为参数,并返回所有int参数中最小的int。我曾尝试通过谷歌搜索和阅读API来了解如何正确实现这一点,但我只做了这一步。有人能帮我从语法上纠正这

我正在寻找一种优雅的方式来表达这个伪代码。对于我的赋值,我无法更改方法签名或参数类型

    private static int smallest(int... nums)
    {
        return Arrays.stream(nums).min().getAsInt();
    }
我所要做的就是从方法调用中获取大量不同的int作为参数,并返回所有int参数中最小的int。我曾尝试通过谷歌搜索和阅读API来了解如何正确实现这一点,但我只做了这一步。有人能帮我从语法上纠正这一点以正确编译和输出吗

我无法用格式正确地发布我的控制台错误,所以我将其作为OP的更新发布。要回答@Marvin,我在编译器中得到了这个错误

Methods1.java:25: error: cannot find symbol
  int small = Arrays.stream(nums).min().getAsInt();
              ^
symbol:   variable Arrays
location: class Methods1
1 error

你差点就拿到了,它是
getAsInt()
而不是
get()

完成:

印刷品:

-2

你差点就拿到了,它是
getAsInt()
而不是
get()

完成:

印刷品:

-2

您可以像这样迭代整个数组

private static int smallest(int[] array)
{
    //check if the array is empty
    if(array.length == 0)
    {
        //handle, whatever happens if the array is empty
        return -1; //maybe you should throw an exception here 
    }

    //storing the smallest found value, start with the first int in the array
    int smallest = array[0];

    //the iteration
    for(int i : array)
    {
        //check if the current checked value is smaller than the smallest found...
        if(i < smallest)
        {
            //...and if it is, set it as the smallest found value
            smallest = i;
        }
    }
    //finally, return the smallest value
    return smallest;
}
私有静态int最小(int[]数组)
{
//检查数组是否为空
如果(array.length==0)
{
//句柄,无论数组为空时发生什么
return-1;//也许您应该在这里抛出一个异常
}
//存储找到的最小值时,从数组中的第一个int开始
int最小=数组[0];
//迭代
for(int i:array)
{
//检查当前检查值是否小于找到的最小值。。。
如果(i<最小值)
{
//…如果是,则将其设置为找到的最小值
最小=i;
}
}
//最后,返回最小值
返回最小;
}

这应该可以解决您当前的问题,但在大多数情况下,我建议使用预先排序的数组或列表。如果其中的数据已按升序存储,则第一个元素始终为最低值,最后一个元素始终为最高值

您可以像这样迭代整个数组

private static int smallest(int[] array)
{
    //check if the array is empty
    if(array.length == 0)
    {
        //handle, whatever happens if the array is empty
        return -1; //maybe you should throw an exception here 
    }

    //storing the smallest found value, start with the first int in the array
    int smallest = array[0];

    //the iteration
    for(int i : array)
    {
        //check if the current checked value is smaller than the smallest found...
        if(i < smallest)
        {
            //...and if it is, set it as the smallest found value
            smallest = i;
        }
    }
    //finally, return the smallest value
    return smallest;
}
私有静态int最小(int[]数组)
{
//检查数组是否为空
如果(array.length==0)
{
//句柄,无论数组为空时发生什么
return-1;//也许您应该在这里抛出一个异常
}
//存储找到的最小值时,从数组中的第一个int开始
int最小=数组[0];
//迭代
for(int i:array)
{
//检查当前检查值是否小于找到的最小值。。。
如果(i<最小值)
{
//…如果是,则将其设置为找到的最小值
最小=i;
}
}
//最后,返回最小值
返回最小;
}

这应该可以解决您当前的问题,但在大多数情况下,我建议使用预先排序的数组或列表。如果其中的数据已按升序存储,则第一个元素始终为最低值,最后一个元素始终为最高值

此方法使用varargs作为参数,接受无限未知的参数量。将从main调用添加的所有参数组合到同一类型的数组中。这是为了说明main中原始方法调用的可变性。最后,返回所有参数中的最小整数

我是一个相当新的程序员,进入计算机科学二年级,我不确定这是否对任何人有用,但我希望它能有所帮助。感谢这里的每一个人给你的精彩提示和错误捕捉。我的问题是我忘记导入我的数组类,并且我从流类调用的一个方法的名称不正确

最后,对于任何有经验的程序员来说,除了看起来简洁优雅之外,这个语句的执行速度是否比执行一个简单的foreach循环并将num与最后一个最小的循环进行比较快

   import java.util.Arrays;

   public class Test
   {
       public static void main(String[] args)
       {
           // Enter as many as you want here, can be more or less than three
           int num1 = 23;
           int num2 = 89;
           int num3 = 9;

           // Apply each variable as an argument for the method call
           int smallestNumber = smallest(num1, num2, num3);
           // Print out variable value to prove it works
           System.out.print(smallestNumber);
       }

       private static Integer smallest(int... nums)
       {
           // Found an elegant way to do the stubbed out block of code
           // I left the code down there to show what is going on here
           try
           {
               return Arrays.stream(nums).min().getAsInt();
           }
           catch (Exception e)
           {
               return null;
           }


           // The above code is essentially doing the below code
           /*try
           {
               // Initialize Variable to start of array
               int smallest = nums[0];

               // For:Each Loop: go through each parameter and assign it a local variable to compare with
               for(int i : nums)
               {
                   // compare if smaller
                   if(i < smallest)
                   {
                       // If true, set as smallest
                       smallest = i;
                   }
               }
               return smallest;
           }
           catch (Exception e)
           {
               return null;
           }*/
       }
    }
导入java.util.array;
公开课考试
{
公共静态void main(字符串[]args)
{
//在此处输入任意数量,可以多于或少于三个
int num1=23;
int num2=89;
int num3=9;
//将每个变量作为方法调用的参数应用
int smallestNumber=最小值(num1、num2、num3);
//打印变量值以证明其有效
系统输出打印(最小数字);
}
私有静态整数最小值(int…nums)
{
//找到了一种优雅的方法来完成代码的存根块
//我把代码放在下面是为了显示这里发生了什么
尝试
{
返回Arrays.stream(nums.min().getAsInt();
}
捕获(例外e)
{
返回null;
}
//上面的代码实际上是在执行下面的代码
/*试一试
{
//将变量初始化为数组的开头
int最小值=nums[0];
//For:Each循环:遍历每个参数,并为其指定一个局部变量进行比较
for(int i:nums)
{
//如果较小,则进行比较
如果(i<最小值)
{
//如果为true,则设置为最小值
最小=i;
}
}
返回最小;
}
捕获(例外e)
{
返回null;
}*/
}
}

此方法使用varargs作为参数,获取无限未知的参数数量。将从main调用添加的所有参数组合到同一类型的数组中。这是为了说明main中原始方法调用的可变性。最后,返回所有参数中的最小整数

我是一个相当新的程序员,进入计算机科学二年级,我是n