使用方法参数查找数组最小值的优雅方法?
Elegant way to find min of an array using method parameters?
我正在寻找一种优雅的方式来表达这个伪代码。对于我的作业,我无法更改方法签名或参数类型。
private static int smallest(int... nums)
{
return Arrays.stream(nums).min().getAsInt();
}
我想要做的就是从方法调用中获取一个巨大的可变列表 ints 作为参数,return 最小的 所有 int 参数中的 int。我已经尝试 google 并阅读了 API 以找出如何正确实现它,但我只得到了这么多。有人可以帮我在语法上更正它以正确编译和输出吗?
我无法正确 post 我的控制台格式错误,所以我 post 将其作为我的 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()
:
private static int smallest(int... nums) {
return Arrays.stream(nums).min().getAsInt();
}
完成working sample on ideone.com:
import java.util.Arrays;
class Ideone {
public static void main (String[] args) {
int[] nums = new int[] { 7, -2, 5, 12 };
System.out.println(smallest(nums));
}
private static int smallest(int... nums) {
return Arrays.stream(nums).min().getAsInt();
}
}
打印:
-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;
}
这应该可以解决您当前的问题,但在大多数情况下,我宁愿建议改用预先排序的数组或列表。如果其中的数据已经按升序存储,则第一个元素始终是最低值,最后一个元素始终是最高值。
此方法通过使用 varargs 作为参数来获取无限未知变量的参数。将从 main 调用中添加的所有参数组合到相同类型的数组中。这是为了解决 main 中原始方法调用的可变性而设计的。最后,返回所有参数的最小整数。
我是一名相当新的程序员,计算机科学专业二年级,我不确定这对任何人是否有用,但我希望它能有所帮助。感谢这里的每个人提供的精彩提示和错误捕获。我的问题是我忘记导入数组 class 并且来自流 class 的方法调用之一被错误命名。
最后,对于任何经验丰富的程序员来说,除了这个看起来活泼和优雅之外,这个语句的执行速度是否比执行简单的 foreach 循环并将 num 与最后一个最小的 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;
}*/
}
}
我正在寻找一种优雅的方式来表达这个伪代码。对于我的作业,我无法更改方法签名或参数类型。
private static int smallest(int... nums)
{
return Arrays.stream(nums).min().getAsInt();
}
我想要做的就是从方法调用中获取一个巨大的可变列表 ints 作为参数,return 最小的 所有 int 参数中的 int。我已经尝试 google 并阅读了 API 以找出如何正确实现它,但我只得到了这么多。有人可以帮我在语法上更正它以正确编译和输出吗?
我无法正确 post 我的控制台格式错误,所以我 post 将其作为我的 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()
:
private static int smallest(int... nums) {
return Arrays.stream(nums).min().getAsInt();
}
完成working sample on ideone.com:
import java.util.Arrays;
class Ideone {
public static void main (String[] args) {
int[] nums = new int[] { 7, -2, 5, 12 };
System.out.println(smallest(nums));
}
private static int smallest(int... nums) {
return Arrays.stream(nums).min().getAsInt();
}
}
打印:
-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;
}
这应该可以解决您当前的问题,但在大多数情况下,我宁愿建议改用预先排序的数组或列表。如果其中的数据已经按升序存储,则第一个元素始终是最低值,最后一个元素始终是最高值。
此方法通过使用 varargs 作为参数来获取无限未知变量的参数。将从 main 调用中添加的所有参数组合到相同类型的数组中。这是为了解决 main 中原始方法调用的可变性而设计的。最后,返回所有参数的最小整数。
我是一名相当新的程序员,计算机科学专业二年级,我不确定这对任何人是否有用,但我希望它能有所帮助。感谢这里的每个人提供的精彩提示和错误捕获。我的问题是我忘记导入数组 class 并且来自流 class 的方法调用之一被错误命名。
最后,对于任何经验丰富的程序员来说,除了这个看起来活泼和优雅之外,这个语句的执行速度是否比执行简单的 foreach 循环并将 num 与最后一个最小的 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;
}*/
}
}