Logo

Programming-Idioms

History of Idiom 7 > diff from v126 to v127

Edit summary for version 127 by programming-idioms.org:
Restored version 124: No.

Version 126

2022-02-13, 18:12:54

Version 127

2022-02-14, 07:39:04

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Variables
i,x,items
Variables
i,x,items
Extra Keywords
indices traverse traversal
Extra Keywords
indices traverse traversal
Code
class test
{
	public static void main (strimg[] args)
	{
		int arr1[] = {1,2,3};
		int arr2[] = {1,2,3};
		if (arrays.equals(arr1,arr2))
		system.out.println("same");
		else
		system.out.println("same");
	}
}
Code
items.eachWithIndex {
  x, i -> println "Item $i = $x"
}
Demo URL
https://groovyconsole.appspot.com/script/5084818014470144
Demo URL
https://groovyconsole.appspot.com/script/5084818014470144
Code
class test
{
	public static void main (strimg[] args)
	{
		int arr1[] = {1,2,3};
		int arr2[] = {1,2,3};
		if (arrays.equals(arr1,arr2))
		system.out.println("same");
		else
		system.out.println("same");
	}
}
Code
for (int i = 0; i < items.length; i++) {
    T x = items[i];
    System.out.printf("Item %d = %s%n", i, x);
}
Comments bubble
items is an array.
Comments bubble
items is an array.