Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #351 Create a zipped collection

Generate a "zipped" list z of pairs of elements from the lists a, b having the same length n.

The result z will contain n pairs.

z = zip(a, b)
import static java.util.Arrays.stream;
Object[] zip(Object[] ... c) {
    Object[] t, T;
    int a, b, i, m = c.length,
        z = stream(c).mapToInt(x -> x.length)
                     .max().getAsInt();
    t = new Object[z];
    for (b = i = 0; b < z; ++b) {
        T = new Object[m];
        for (a = 0; a < m; ++a)
            if (b < c[a].length)
                T[a] = c[a][b];
        t[i++] = T;
    }
    return t;
}
z = a.zip(b)

New implementation...
< >
reilas