Reputation: 59
I'm looking for an elegant way to express this pseudo code. For my assignment, I cannot change the method signature or parameter type.
private static int smallest(int... nums)
{
return Arrays.stream(nums).min().getAsInt();
}
All I'm trying to do is take a huge varying list of ints as a parameter from the method call and return the smallest int of all the int parameters. I've tried to google and read the API to find out how to correctly implement this, but I've gotten only this far. Can someone help me syntactically correct this to compile and output correctly?
I cannot correctly post my console errors with formatting, so I'm posting it as an update to my OP. To answer @Marvin I'm getting this error in my compiler...
Methods1.java:25: error: cannot find symbol
int small = Arrays.stream(nums).min().getAsInt();
^
symbol: variable Arrays
location: class Methods1
1 error
Upvotes: 0
Views: 1292
Reputation: 59
This method takes an infinite unknown variable quantity of parameters by using varargs as it's argument. Combining all the parameters added from it's call from main into an array of the same type. This was designed to account for mutability of the original method call in main. Finally, returning the smallest integer of all the parameters.
I'm a fairly new programmer, second year into computer science, and I'm not sure if this is even useful for anyone, but I hope it helps. Thanks to everyone here for your awesome tips and error catches. My issue was I forgot to import my Array class and one of my method calls from stream class was incorrectly named.
Lastly, for any veteran programmers out there, aside from this looking snappy and elegant, does this statement execute any faster than doing a simple foreach loop and comparing the num to the last smallest one?
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;
}*/
}
}
Upvotes: 0
Reputation: 14425
You almost had it, it's getAsInt()
instead of get()
:
private static int smallest(int... nums) {
return Arrays.stream(nums).min().getAsInt();
}
Complete 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();
}
}
Prints:
-2
Upvotes: 4
Reputation: 134
You could iterate over the whole array like this
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;
}
This should solve your current problem, but in most cases i'd rather recommend to use a pre sorted array or list instead. If the data in it is already stored in ascending order, the first element would always be the lowest and the last element always the highest value.
Upvotes: 1