Java Programming Tutorial – 31 – Enhanced for Loop

///Java Programming Tutorial – 31 – Enhanced for Loop

Java Programming Tutorial – 31 – Enhanced for Loop

FavoriteLoadingAdd to favorites

Website –
GitHub –
Reddit –
Twitter –
Facebook –
LinkedIn –
Instagram –

source

Java468x60
By |2020-12-30T11:29:01+00:00December 30th, 2020|Java Video Tutorials|23 Comments

23 Comments

  1. OwlNator December 30, 2020 at 11:29 am - Reply

    Easily and nicely explained; thank you.

  2. Prithvi Shan December 30, 2020 at 11:29 am - Reply

    thank you bucky 😉 helped me a lottt

  3. Stranac Oppilas December 30, 2020 at 11:29 am - Reply

    A video helping people, 11 years from the future, make sense of how the enhanced for loop works. Thanks!

  4. Dishie December 30, 2020 at 11:29 am - Reply

    he made this at 2:45 am…

  5. Hillary Jude December 30, 2020 at 11:29 am - Reply

    Even after 10 years of this video I still prefer @thenewboston. Explanations are crystal clear.

  6. Molly Percocets December 30, 2020 at 11:29 am - Reply

    Did the array declaration changed?

    Is it the same to declare an array like
    “`int[] bucky = {}“`

  7. Agent Smith December 30, 2020 at 11:29 am - Reply

    THANK YOU!!!!!!!!!

  8. Tiger December 30, 2020 at 11:29 am - Reply

    You can't use x variable as a counter(for counting the iterations) in an enhanced loop (foreach loop) , since it is storing and printing the array items. //for(int x: freq)

    If you tried to print the array items using freq[x] , it will go out of IndexBound most likely
    For eg-
    Random rand = new Random();
    int freq[ ] = new int[6];
    for(int i=0;i<1000;i++) {
    ++freq[rand.nextInt(6)];
    }

    System.out.println("FacestFrequency");

    for(int x: freq) {
    System.out.println((x+1) + "t" + freq[x]);
    }

    so its like printing freq[ freq[ i ] ];
    //which will give error as
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 148 out of bounds for length 6
    //WHAT YOU CAN DO INSTEAD IS

    Random rand = new Random();
    int freq[ ] = new int[6];
    for(int i=0;i<1000;i++) {
    ++freq[rand.nextInt(6)];
    }

    System.out.println("FacestFrequency");
    int counter =0;
    for(int x: freq) {

    System.out.println((++counter) + "t" + x);
    }
    I used a new variable named counter to keep track of the iterations.

  9. Jason 555 December 30, 2020 at 11:29 am - Reply

    My compiler is set to 1.8 by default but I still get a Syntax error at "total += x;"
    Can anyone help?

  10. TBoneAPH December 30, 2020 at 11:29 am - Reply

    Thanks! This clarified things.

  11. Arnav PVPKumar December 30, 2020 at 11:29 am - Reply

    With this lesson, I've created 2 different random number generators that add the sums of the numbers. One has a for loop that will repeat it's for loop code (with a incrementation) a random number of times, while the other uses an array with 4 random values added together. I'll share both of them for anyone who may want to use them to analyse the way the java randomiser functions (because I did see some odd similarities (for instance, the value 0 being repeated quite often compared to what you'd expect considering how low the probability of that occurring is because of all the possible values the randomiser can input)).

    The "completely" random code:
    import java.util.Random;

    public class RandomNumberGenerator

    {

    public static void main(String args[]){

    Random num1 = new Random ();

    Random num2 = new Random ();

    int add = 0;

    int somenum = num1.nextInt();

    for(int a = 1; a<=somenum; a++){

    add += num2.nextInt();

    }

    System.out.println("The sum of your random numbers is: "+add);

    }

    }

    The code which uses 4 random values added together:
    import java.util.Random;

    public class ThisIsFun

    {

    public static void main(String args[]){

    Random a = new Random ();

    Random b = new Random ();

    Random c = new Random ();

    Random d = new Random ();

    int i = 0;

    int e = a.nextInt();

    int f = b.nextInt();

    int g = c.nextInt();

    int h = d.nextInt();

    int Jimmy[] = {e, f, g, h};

    for(int j: Jimmy){

    i += j;

    }

    System.out.println("The sum of the 4 random values is: "+i);

    }

    }

    Note: I also do realize that my codes could've been written more elegantly, but that wasn't my aim.

    Thank you for your time!

  12. Ryan Goo December 30, 2020 at 11:29 am - Reply

    2019 boys and girls!!

  13. December 30, 2020 at 11:29 am - Reply

    I wish you provided sample questions that may use this array loop at end of video or in description or something

  14. Aava Sapkota December 30, 2020 at 11:29 am - Reply

    So basically, an enhanced for loop has a value to store stuff and goes through the length of something else with the limits set of the length

  15. King Og Of bashan December 30, 2020 at 11:29 am - Reply

    He's voice is very buckish

  16. Christopher Harwell December 30, 2020 at 11:29 am - Reply

    play this at .50 speed you wont regret it haha

  17. FIVE STAR GAMING December 30, 2020 at 11:29 am - Reply

    for everybody's kind information , it's called a FOREACH Loop

  18. Raj Yadav December 30, 2020 at 11:29 am - Reply

    Sir plzess Speak in hindi

  19. Milad Alamir Ahmed December 30, 2020 at 11:29 am - Reply

    but how can I use this to display all the values of the arrays instead of the total?

  20. Allen Farias December 30, 2020 at 11:29 am - Reply

    //one for strings
    public class apple {

    public static void main(String[] args) {

    String str[] = {"alex", "allen", "daniel"};

    for(String x: str) {

    if(x.equals("allen")) {

    System.out.println(x + " is allen");

    }else {

    System.out.println(x + " is not allen");

    }

    }

    }

    }

  21. Saulius Ogenskas December 30, 2020 at 11:29 am - Reply

    Is x always called x ?

  22. Mark Bisny December 30, 2020 at 11:29 am - Reply

    import java.util.Random;

    //import java.util.Scanner;
    class Tutorial {

    public static void main(String[] args) {

    int bucky[]= {3, 4 ,5 ,6, 7};
    int total=0;

    for(int x: bucky ) {
    total+=x;
    }
    System.out.println(total);

    }

    }

  23. Daiana Bilbao December 30, 2020 at 11:29 am - Reply

    Can you change the values of the elements of array bucky with a for each loop? What I am thinking is that to change the element values in an array then you need to refer to the element with it's index and it seems like that isn't possible with a for each loop? Since a for each loop doesn't use indexes. I am confused about this.

Leave A Comment

*