• Register
3 votes
19 views

Problem:

Hello good souls,

I am a new student of Java. I just started learning and trying to solve java problems. Right now, I am working with the arrays. I can do some basic things with an array. I am finding something more advance problems of an array. Thus I thought to find the maximum and minimum value in an array. Still, I don’t know is it an advance level problem or not but for me it is! So, my question is simple, how to find minimum and maximum values in an array in java?

Let’s say I have an array

Integer[] num = { 2, 4, 6, 5, 8 }; 

Thanks in advance for your effort.

12 7 7
15,250 points

1 Answer

0 votes

Solution:

Thanks for your specific and to the point question. You asked the question very clearly. Well, you have an array declared in your program, you can easily find out the Minimum and Maximum value of it. You can either use iteration or pre-defined function to do this.

As you said in your question, you want to proceed to the advance level, I decided to show you it by using a function. I hope you already know how to use or handle an iteration.

So, let’s have a look into the program below:

import java.util.Arrays; 
import java.util.Collections; 
  
public class MinNMax { 
public static void main(String[] args) 
    { 
  
        Integer[] num = { 2, 4, 6, 5, 8 }; 
        int min = Collections.min(Arrays.asList(num)); 
        int max = Collections.max(Arrays.asList(num)); 
  
        System.out.println("The minimum number of this array is : " + min); 
        System.out.println("The maximum number of this array is : " + max); 
    } 
} 

aslist() typecasts a list from the array passed in its argument. This function is defined in “Java.utils.Arrays“ library. To bring out the maximum and minimum number from the array I used Collections.max() and Collections.min() methods.

Thanks

13 9 6
94,240 points

Related questions

1 vote
1 answer 12 views
12 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
asked Mar 29 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Maximum and minimum in a C ++ vector with numbers from a file I'm trying to make a C ++ program that sends 100 random numbers to a file, then collects them and gets the largest number and the smallest number. I have tried to do this by putting the hundred numbers in a vector and taking the ...   printf ("The greater is% d \ n", greater);         printf ("The minor is% d \ n", minor);     } }
asked Aug 15 game 4.3k points
0 votes
1 answer 12 views
12 views
I want to know how to sort an array in java without using sort method.
asked Sep 28 Daniel Anderson 4k points
3 votes
2 answers 1.5K views
1.5K views
Problem: I am struggling with a problem for a few hours back. I know how to sort an array by using methods. I was looking for another way (perhaps a loop) to do it more efficiently. Let me put my question this way, how to sort an array in java without using sort method? Could anybody here please help to make this happen? Thanks in advance
asked Mar 24 Gavin 15.3k points
1 vote
1 answer 19 views
19 views
Problem: Hello guys, I have a very simple question regarding Java Programming. I’ve searched your website before posting this question but I haven't got any appropriate answer. Yes, there are a couple of answers but not as specific as I needed. I want to generate some random numbers in an array of Java programs. So, help me to fill an array with random numbers java. Many thanks...
asked Apr 6 Gavin 15.3k points