+ 2
what is foreach loop and its uses?
how can we use for each loop examples and syntax?
2 Antworten
+ 5
If arr is an integer array, then you can access it using the following foreach loop:
for(int item: arr) {
System.out.println(item);
}
It means you're defining each element of that array as item. So you can access the elements using item instead of arr [index]. foreach loop checks all elements of a collection from first to last, so you don't need the concept of index to access elements.
So the basic syntax is:
for(type variable_name : collection_name){
// access using variable_name
}
+ 4
Here's some code that can maybe help you.
https://code.sololearn.com/c07Z38dDsU79/?ref=app