• Register
search
Log In
3 votes
18 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.

15.2k 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

87.3k points

Related questions

1 vote
1 answer 11 views
11 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.2k points
3 votes
2 answers 21 views
21 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.2k points
1 vote
1 answer 15 views
15 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.2k points
1 vote
1 answer 14 views
14 views
Problem: Hello Kodlogs, I find this website very helpful besides the StackOverflow. You guys are really awesome and friendly. I want to know from you guys, how to pass an array to a method? Also what happens when an array is passed to a method? Thanks for your valuable and patient answer.
asked Mar 31 Gavin 15.2k points
1 vote
1 answer 18 views
18 views
I'm facing problem in java to declare an array also I'm trying to declare values of the declared array but I'm getting a compelling error every time. I don't know why this happening please help me to write statements to declare the array and assign the values to the ... in IDE “Error:(8, 9) java: variable array might not have been initialized”, Please help me how can I fix that issue?
asked May 3 samhaz 5k points