Find the Maximum and Minimum values in an Array (Java)

preview_player
Показать описание
A simple algorithm to help you find the maximum and minimum values inside of an array.
Рекомендации по теме
Комментарии
Автор

2022, still using the tutorial. Super helpful.

allfine.
Автор

Thanks really needed this for an assignment

jaimecasella
Автор

Surely you'd just want to declare "int i = 1"in your for loop, since you've already initialised variable largest with values[0]

Desiqnify
Автор

I already know how to sort the values of the array, how do i compare String values to the same object sorted????

String names[]
double scores[]

please assist

Nyhmii
Автор

The code doesn't work for an array containing {5.6, 3.4, 1.2, 7.8} gives me 5.6 as the max

cianorourke
Автор

thansk but i think you should have started you loop for at 1 not 0 for more efficiency as you already assign the min to values[0]

bergamobobson
Автор

How would you do it in for each loop ?

strongminds
Автор

Did you come up with this code yourself?
Im working on a project and a part required for me to do the same I was pretty close haha

motoduster
Автор

does this also work for an ArrayList ?

tataijames
Автор

why is it wrong to put value[i]=largest?? why must it be largest=value[i]

Chandler
Автор

Please tell what is wrong in this
The smallest is coming 0
import java.io.*;
public class Array_1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
int a[]=new int [20];
int max=a[0];
int min=a[0];
for (int i=0;i<20;i++)
{
System.out.println("Enter a number");

if (a[i]>max)
max=a[i];
if (a[i]<min)
min=a[i];
}
System.out.println("The highest no. is : "+max);
System.out.println("The lowest no. is : "+ min);
}
}

soubhadra