Logo

Programming-Idioms

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.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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 = zip(a, b)
z = a.zip(b)