Java >> Tutoriel Java >  >> Java

Java pour chaque programme de boucle

Dans ce didacticiel, nous allons apprendre à implémenter une boucle for-each dans différents scénarios. Mais avant d'aller plus loin, si vous n'êtes pas familier avec le concept de boucle for-each, consultez l'article sur les boucles en Java.

Syntaxe :

for(data_type variable : array | collection)
{  
//body of for-each loop  
}  

Programme 1 :Programme de boucle Java For-each

Dans ce programme, nous verrons comment imprimer les éléments du tableau en utilisant une boucle pour chaque avec des valeurs prédéfinies.

Algorithme :

  1. Commencer
  2. Déclarez un tableau.
  3. Initialiser le tableau.
  4. Parcourir le tableau en utilisant une boucle pour chaque.
  5. Afficher les éléments du tableau.
  6. Arrêter

Ci-dessous le code pour le même

//Java Program to see the implementation of the for-each loop in Arrays
public class Main
{
     public static void main(String []args)
     {
        int arr[]={1,3,4,7,8,5,4};  
        System.out.println("The Array elements are ");
        //traversing the array with for-each loop  
        for(int i:arr)
        {  
           System.out.println(i);  
        }  
     }
}


Les éléments du tableau sont
1
3
4
7
8
5
4

Programme 2 :Programme de boucle Java For-each

Dans ce programme, nous verrons comment imprimer les éléments de la collection en utilisant une boucle pour chaque avec des valeurs prédéfinies.

Algorithme :

  1. Commencer
  2. Déclarez une ArrayList de type chaîne.
  3. Ajouter des éléments à la ArrayList.
  4. Parcourir la ArrayList en utilisant une boucle pour chaque.
  5. Imprimer les éléments ArrayList.
  6. Arrêter

Vous trouverez ci-dessous le code correspondant.

//Java Program to see the implementation of the for-each loop in Collections
import java. util.*;  
public class Main
{  
  public static void main(String args[])
  {  
   //Creating a list of elements  
   ArrayList<String> list=new ArrayList<String>();  
   list.add("Cat");  
   list.add("Dog");  
   list.add("Cow"); 
   list.add("Tiger"); 
   list.add("Lion"); 
   //traversing the list of elements using for-each loop 
   System.out.println("The elements in the list are: ");  
   for(String str:list)
   {  
     System.out.println(str);  
   }  
  
 }   
}  


Les éléments de la liste sont :
Chat
Chien
Vache
Tigre
Lion

Différence entre Java For-Loop et For-each Loop

Ici, nous verrons comment une boucle for-each diffère d'une boucle for. La principale différence entre la boucle for et la boucle for each est que la boucle for nous offre la possibilité de contrôler le processus de bouclage.

Programme 3 :Programme de boucle Java For-each

Dans ce programme, nous verrons l'implémentation d'une boucle for-each dans un programme défini par l'utilisateur.

Algorithme :

  1. Commencer
  2. Créez une instance de la classe Scanner.
  3. Déclarez un tableau.
  4. Demandez à l'utilisateur d'initialiser le tableau à l'aide d'une boucle pour chaque.
  5. Imprimez les éléments du tableau en utilisant le même pour chaque boucle.
  6. Arrêtez.

Vous trouverez ci-dessous le code correspondant.

//Java Program to see the implementation of for-each loop
import java.util.*;  
public class Main
{  
  public static void main(String args[])
  {  
     //Take input from the user
     //Creates an instance of the Scanner Class
    Scanner sc=new Scanner(System.in);
    int arr[]=new int[5];     //Declare an array
    System.out.println("The elements in the array: ");
    for (int i : arr) 
    {
        i=sc.nextInt();      //Initialize the array elements
        System.out.println(i);   //Print the array elements
    }
 }   
}  


Les éléments du tableau :
4
5
3
2
1

Programme 4 :Programme Java For Loop

Dans ce programme, nous verrons l'implémentation d'une boucle for dans un programme défini par l'utilisateur.

Algorithme :

  1. Commencer
  2. Créez une instance de la classe Scanner.
  3. Déclarez un tableau.
  4. Demandez à l'utilisateur d'initialiser le tableau à l'aide d'une boucle for.
  5. Afficher les éléments du tableau en utilisant la même boucle for.
  6. Arrêtez.

Vous trouverez ci-dessous le code correspondant.

//Java Program to see the implementation of for loop
import java.util.*;  
public class Main
{  
  public static void main(String args[])
  {  
     //Take input from the user
     //Creates an instance of the Scanner Class
    Scanner sc=new Scanner(System.in);
    int arr[]=new int[5];     //Declare an array 
    //Initialize the array elements
    System.out.println("Initializing the elements in the array: ");
    for (int i=0; i< arr.length;i++) 
    {
        arr[i]=sc.nextInt();
    }
    //Print the array elements
    System.out.println("The elements in the array: ");
    for (int i=0; i<arr.length;i++) 
    {
        System.out.println(arr[i]);
    }
 }   
}  


Initialisation des éléments du tableau :4 5 3 2 1
Les éléments du tableau :
4
5
3
2
1


Balise Java