for-each loop in java

for-each loop in java:

 

for-each loop in java basically design for traversing data from an array

we can use for-each with both type of array one –D array as well as two-D array.

But firsto fall we need to understand working and syntax of for-each loop .

Workflow diagram of for-each loop:

for-each loop syntax with One-D Array:

  for(data type variable name:One-D array)

  {

     //statement

  }

 

  Now time is to understand above syntax:

 

  First:          0   1  2

int [] arr={10,20,30};

  Second:

for(int i:)

this means data type of variable should be match with data type of array

  Third:

for(int i: arr)

  Fourth:
  for(int i: arr)

  {

    System.out.println("value of i is::"+i);

  }

this loop will run till (n-1) n=size of array and for this array size is 3

here i will print value of  index like [0]=10 ,[1]=20, [2]=30

 

  Example:

 

 

  class foreachwithOne_DArray

  {

    public static void main(String [] ar)

                {

      int [] arr={10,20,30};  //declaration of One-D Array

                 

                  for(int i:arr) //write for-each loop for traversing data from an One-D array

                  {

                   System.out.println("value of i is::"+i); // i will print value from array

                  }

                }

  }


  Output:

  value of i is::10

  value of i is::20

  value of i is::30

 

 

  for-each loop syntax with Two-D Array:

 

   for(data type  [ ]  name of One-D Array: Two-D Array)

    {

        for(data type variable name:One-D array)

         {

            //statement

          }

  }


  Now time is to understand above syntax:

 

We know that whenever we work on two d array then use nested loop first loop manage

row count and second loop manage column count.

 First:

int [] [] arr={{10,20,30}, {50,35,34}}; // Two-D Array declaration in java

(arr) Structure

0 1 2
 0 10 20 30
1 50 35 34

 

above array structure for two-d array.

Second:

for(data type  [ ]  name of One-D Array: Two-D Array)   // for(int [] arr1:arr) {  }

so in first iteration first row of array index (0) copy in  One-D array and this thing happen till

(n-1) here n is number of row count.

So structure of One-d array now is:

First Iteration:
0 1 2
0 10 20 30

 

Second Iteration:
0 1 2
0 50 35 34

 

Third:

for(data type variable name:One-D array) // for(int i:arr1) {  }

Example:

class foreachwithTwoDArray

                 {

    public static void main(String [] ar)

    {

            int [] [] arr={{10,20,30}, {50,35,34}};

          for(int [] arr1:arr)

            {

                 for(int k:arr1)

                 {

              System.out.print(k);

                  }                                                           

                   System.out.print("\n");

        }    } }


Output:

10 20 30
50 35 34

 

 

Hope so you like this article Thanks…

45 thoughts on “for-each loop in java

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. I simply want to say I am just all new to blogging and site-building and seriously enjoyed this website. Very likely I’m want to bookmark your site . You definitely have beneficial writings. Thanks a lot for sharing your web site.

  2. Hi! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone4. I’m trying to find a template or plugin that might be able to resolve this problem. If you have any suggestions, please share. With thanks!

  3. Hey! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be great if you could point me in the direction of a good platform.

  4. Hi, Neat post. There is a problem with your website in internet explorer, would test this… IE still is the market leader and a good portion of people will miss your excellent writing because of this problem.

  5. Can I simply say what a relief to seek out someone who actually is aware of what theyre speaking about on the internet. You definitely know the right way to bring a difficulty to light and make it important. More people must read this and understand this aspect of the story. I cant imagine youre no more in style because you undoubtedly have the gift.

  6. Thank you a lot for providing individuals with an exceptionally memorable possiblity to read critical reviews from here. It is always so excellent plus stuffed with a lot of fun for me and my office co-workers to visit your website on the least three times a week to read the new guides you will have. Not to mention, we’re always impressed for the staggering thoughts served by you. Selected two facts in this posting are in reality the most impressive I’ve had.

  7. Your style is unique in comparison to other folks I’ve read
    stuff from. Many thanks for posting when you have the opportunity, Guess I will just bookmark this blog.

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.