Logo

Programming-Idioms

History of Idiom 7 > diff from v152 to v153

Edit summary for version 153 by reilas:
New Java implementation by user [reilas]

Version 152

2024-11-03, 21:51:12

Version 153

2024-11-09, 02:28:33

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
Imports
import java.util.stream.IntStream;
Code
record E<T>(int i, T t) {
    @Override
    public String toString() {
        return "(%s, %s)".formatted(i, t);
    }
}
@SuppressWarnings("unchecked")
<T> E<T>[] enumerate(T... a) {
    return IntStream.range(0, a.length)
                    .mapToObj(i -> new E<>(i, a[i]))
                    .toArray(E[]::new);
}