Java 程序:for-each循环

原文:https://www.studytonight.com/java-programs/java-for-each-loop-programs

在本教程中,我们将学习如何在不同的场景中实现 for-each 循环。但是在继续之前,如果你不熟悉 for-each 循环的概念,那么一定要查看一下 Java 中Loops的文章。

语法:

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

程序 1:Java For-每个循环程序

在这个程序中,我们将看到如何使用带有预定义值的 for 循环来打印数组元素。

算法:

  1. 开始
  2. 声明一个数组。
  3. 初始化数组。
  4. 通过对每个循环使用遍历数组。
  5. 打印数组元素。
  6. 停止

下面是相同的代码

//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);  
        }  
     }
}

阵元为 1 3 4 7 8 5 4

程序 2:Java For-每个循环程序

在本程序中,我们将看到如何使用预定义值的每个循环打印集合元素。

算法:

  1. 开始
  2. 声明String类型的ArrayList
  3. ArrayList中添加元素。
  4. 每个循环使用一个遍历ArrayList
  5. 打印ArrayList元素。
  6. 停止

下面是相同的代码。

//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);  
   }  

 }   
}

列表中的元素有: 猫 狗 牛 虎 狮

Java For 循环和 For 每个循环的区别

在这里,我们将看到 for-each 循环与 for 循环有何不同。for 循环和每个循环之间的主要区别在于,for 循环为我们提供了控制循环过程的机会。

Java 程序:程序 3:每个循环

在这个程序中,我们将看到在用户定义的程序中每个循环的实现。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明一个数组。
  4. 要求用户为每个循环使用初始化数组。
  5. 对每个循环使用相同的打印数组元素。
  6. 停下来。

下面是相同的代码。

//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
    }
 }   
}

阵中元素: 4 5 3 2 1

程序 4: Java For 循环程序

在这个程序中,我们将看到在用户定义的程序中 for 循环的实现。

算法:

  1. 开始
  2. 创建 Scanner 类的实例。
  3. 声明一个数组。
  4. 要求用户使用 for 循环初始化数组。
  5. 使用相同的 for 循环打印数组元素。
  6. 停下来。

下面是相同的代码。

//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]);
    }
 }   
}

初始化数组中的元素:4 5 3 2 1 数组中的元素: 4 5 3 2 1