English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Java

Contrôle de flux Java

Java tableau

Java orienté objet (I)

Java orienté objet (II)

Java orienté objet (III)

Gestion des exceptions Java

Java Liste (List)

Java Queue (file d'attente)

Java Map collection

Java Set collection

Java entrée/sortie (I/O)

Java Reader/Writer

Autres sujets Java

Recherche de l'élément le plus grand dans un tableau par le programme Java

Comprehensive list of Java examples

Dans ce programme, vous apprendrez à utiliser la boucle for en Java pour trouver l'élément le plus grand dans un tableau.

Example: find the maximum element in the array

public class Largest {
    public static void main(String[] args) {
        double[] numArray = { 23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7, -66.5 };
        double largest = numArray[0];
        for(double num: numArray) {
            if(largest < num)
                largest = num;
        }
        System.out.format("Maximum element = %.2f", largest);
    }
}

When running the program, the output is:

Maximum element = 55.50

In the above program, we store the first element of the array in the variable largest.

Then, largest is used to compare other elements in the array. If any number is greater than largest, a new number is assigned to largest.

Thus, the data of the maximum element will be stored in largest.

Comprehensive list of Java examples