xxxxxxxxxx
public class ForeachLoop : MonoBehaviour
{
void Start ()
{
string[] strings = new string[3];
strings[0] = "First string";
strings[1] = "Second string";
strings[2] = "Third string";
foreach(string item in strings)
{
print (item);
}
}
}
xxxxxxxxxx
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
xxxxxxxxxx
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
xxxxxxxxxx
// definition eine Datenstruktur, hier ein Array mit 9 Werten
int[] array = new int[]{4, 8, 4, 2, 2, 1, 1, 5, 9};
// ForEach Schleife
for( int k: array )
{
System.out.println("k = "+k);
}
xxxxxxxxxx
public class Sample {
public static void main(String[] args) {
String names[] = { "Jasmine", "Lyka", "Marbie", "Soleen", "Tiny" };
for (String name : names) {
System.out.println(name);
}
}
}
xxxxxxxxxx
String[] fruits = {"apples", "oranges", "pears", "plums"}
for (String i:fruits)
{
System.out.print(i);
}
xxxxxxxxxx
int [] intArray = { 10, 20, 30, 40, 50 };
for( int value : intArray ) {
System.out.println( value );
}
xxxxxxxxxx
ArrayList<Item> itemList = new ArrayList<>()
itemList.add(item1);
itemList.add(item2);
itemList.add(item3;
for (Item item : itemList){
System.out.println(item);
}
xxxxxxxxxx
const a = ["a", "b", "c"];
for (const val of a) { // You can use `let` instead of `const` if you like
console.log(val);
}
xxxxxxxxxx
for (List<String> item : items) {
for (String s : item) {
System.out.println(s);
}
}
//the for-each loop:
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
for (int number : numbers) {
System.out.println(number);
}