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)]; }
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.
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.
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.
Easily and nicely explained; thank you.
thank you bucky 😉 helped me a lottt
A video helping people, 11 years from the future, make sense of how the enhanced for loop works. Thanks!
he made this at 2:45 am…
Even after 10 years of this video I still prefer @thenewboston. Explanations are crystal clear.
Did the array declaration changed?
Is it the same to declare an array like
“`int[] bucky = {}“`
THANK YOU!!!!!!!!!
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.
My compiler is set to 1.8 by default but I still get a Syntax error at "total += x;"
Can anyone help?
Thanks! This clarified things.
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!
2019 boys and girls!!
I wish you provided sample questions that may use this array loop at end of video or in description or something
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
He's voice is very buckish
play this at .50 speed you wont regret it haha
for everybody's kind information , it's called a FOREACH Loop
Sir plzess Speak in hindi
but how can I use this to display all the values of the arrays instead of the total?
//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");
}
}
}
}
Is x always called x ?
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);
}
}
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.