• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

finding the lowest of 4 values

If I have values A, B, C and D
and I wish to find which has the lowest value.

What would be the best way to do this?
0
jtiernan2008
Asked:
jtiernan2008
2 Solutions
 
objectsCommented:
You can use Math.min()

min = Math.min(A, Math.min(B, ....

or use if

int min = Integer.MAX_VALUE;
if (A
0
 
jtiernan2008Author Commented:
so this would be

min=Math.min(A,B,C,D);
0
 
VenabiliCommented:
No, you can have only two parameters so as objects showed above, you need to nest them
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
objectsCommented:
min only takes two params so you need to call it muliple times

0
 
aman123_123Commented:
Here are two ways to achieve the same.

Using list and sort it:
List<Integer> list = new ArrayList<Integer>();
        list.add(100);
        list.add(10);
        list.add(15);
        Collections.sort(list);
        for(int a:list)
            System.out.println(a);
       

Using Sorted set i.e tree set
        Set<Integer> set = new TreeSet<Integer>();
        set.add(100);
        set.add(10);
        set.add(15);
       
        for(int a:set)
            System.out.println(a);


First element of these collections will always be the minimum.
0
 
jtiernan2008Author Commented:
thanks a million
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now