import java.io.*;
import java.util.*;
public class E11 extends Thread {
public void run() {
ArrayList<Integer> primos = new ArrayList<Integer>();
boolean primo = true;
for (int i = 1; i <= 100; i++) {
for (Integer numero: primos) {
if (numero != 1 && i % numero == 0) {
primo = false;
break;
}else {
primo = true;
}
}
if (primo) {
primos.add(i);
System.out.print(i + " ");
}
}
}
public static void main(String[] args){
Thread primo = new E11();
primo.start();
}
}
Good with the following code that I put above, I know what it says about the first 100 integers, those that are primes, but I don't quite understand the for . Can someone explain that for to me?
Thanks in advance
In java there are several ways to iterate. Both are known as
for
andforeach
; the latter is used for collections of objects (Arrays, lists and other data structures) .In this case, the
for (Integer numero: primos)
is known asforeach
in other programming languages. This can also be written asfor(int i = 0; i < primos.size(); i++)
. Both do exactly the same thing, the difference is that in the second one, to obtain an object insideArrayList<Integer>
it is done with the methodget(i)
, while in the first one(foreach)
it directly extracts the object where it is iterating, it is saved innumero
, and automatically when it passes to the next item, numbers will be the next item.The syntax of the foreach in java, or the for to iterate collections of objects is the following:
Where:
Class
is the class to which the collection of objects in the list to iterate belongs to, in your caseInteger
, since the list is going to store objects of this class.x
is the variable to which each item in the list will be assigned (we will see this later in more detail how it works)a
is the name of the list or collection, which must be declared and instantiated.A simple example, let's say we want to print 5 numbers inside a list, and we have 5 elements in a
ArrayList
:With the
for
normal:for(int i = 0; i < primos.size(); i++)
, to print them, we said that to obtain an object with this method we must use the methodget(i)
that is a member ofArrayList<>
, therefore it is as follows:In this way we make sure that for each change of the for counter, the next element of the list of primes is printed, and so on until the same list ends.
With the foreach:
for (Integer numero: primos)
, it is not necessary to do this, since with itforeach
, or with this type of for, when the iteration ends,numero
it takes the next object from the list, if there is no more, itfor
automatically ends. Using the previous example of printing and the arrangement of numbers, it is as follows:This simple code achieves the same result as above.
Now, the use already depends on your needs, the foreach is used when the search is linear, that is, one by one, when you want to go through the list through all the elements in order as they are in the ArrayList<>. If you want to traverse the list out of order, or for example, from the middle of the list to the end, the normal for is used, not the foreach.
I hope it has served you.