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

# 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
2 Solutions

Commented:
You can use Math.min()

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

or use if

int min = Integer.MAX_VALUE;
if (A
0

Author Commented:
so this would be

min=Math.min(A,B,C,D);
0

Commented:
No, you can have only two parameters so as objects showed above, you need to nest them
0

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

0

Commented:
Here are two ways to achieve the same.

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

Using Sorted set i.e tree set
Set<Integer> set = new TreeSet<Integer>();

for(int a:set)
System.out.println(a);

First element of these collections will always be the minimum.
0

Author 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.