# How to find minimum and maximum values in an array in java

3 votes
21 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.

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

## Related questions

0 votes
1 answer 68 views
68 views
I am new to this. I just want to know how to do that?
1 vote
1 answer 21 views
21 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.
0 votes
1 answer 15 views
15 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);     } }
0 votes
1 answer 12 views
12 views
Problem: Hi there! I have started learning java just recently and I found a problem in a programming exercise in which I have to find the second largest number in a given array using programming. I know how to find the largest number, but I am facing difficulty ... number. Can anybody here help me solve the problem? Also, provide the example program so that I can understand it better. Thanks!
0 votes
1 answer 35 views
35 views
I want to know how to sort an array in java without using sort method.
3 votes
2 answers 4.1K views
4.1K 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
0 votes
1 answer 20 views
20 views
In C ++ I made a function that, given a length, creates an array and returns it. Finally I show their values outside the function. I was investigating and they say that in C ++ it is not possible to return an array, and for this there are two alternatives, one is to return a pointer, and another is to pass ... () {     int * arr = create_array (10);     cout << arr [8] << "" << arr [9] << "\ n"; }
1 vote
1 answer 24 views
24 views
Problem: Hello guys, I have a very simple question regarding Java Programming. I&rsquo;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...
1 vote
1 answer 111 views
111 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 &ldquo;Error:(8, 9) java: variable array might not have been initialized&rdquo;, Please help me how can I fix that issue?
1 vote
1 answer 24 views
24 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.