简体   繁体   English

如何连接两个参数化类型数组(在番石榴中)

[英]How to join two arrays of parametrized types (in guava)

I was looking for a concise way to iterate over two arrays. 我正在寻找一种简洁的方法来迭代两个数组。 Since the arrays are not expected to be large I figured I could just concat them. 由于阵列预计不会很大,我想我可以将它们连接起来。

Unfortunately the Guava invocation looks horrible: 不幸的是,番石榴的调用看起来很可怕:

        Class<? extends MyInterface>[] a2 = ...
        Class<? extends MyInterface>[] a1 = ...

        ObjectArrays.concat(a1, a2,
                (Class<Class<? extends MyInterface>>) MyInterface.class.getClass());

Is it possible to make it more readable? 是否有可能使其更具可读性?

Instead of using ObjectArrays you can combine Arrays.asList and Iterables.concat . 您可以组合Arrays.asListIterables.concat而不是使用ObjectArrays This way you don't need to provide a class name. 这样您就不需要提供类名。

Iterables.concat(Arrays.asList(a1), Arrays.asList(a2))

It will be a lot more readable if you use static imports: 如果使用静态导入,它将更具可读性:

import static com.google.common.collect.Iterables.concat;
import static java.util.Arrays.asList;

...

concat(asList(a1), asList(a2))

I ended up writing something of my own. 我最后写了一些自己的东西。

There's a main method which does all the work: 有一个主要方法可以完成所有工作:

@SuppressWarnings("unchecked")
private static <T> T[] mergeInternal(@Nonnull T[] first,
                            @Nonnull T[] second,
                            @Nullable T[] third,
                            @Nullable T[] fourth,
                            @Nullable T[] fifth,
                            @Nullable T[] sixth) {
    int overallLength = first.length + second.length;
    if (third != null) {
        overallLength += third.length;
    }
    if (fourth != null) {
        overallLength += fourth.length;
    }
    if (fifth != null) {
        overallLength += fifth.length;
    }
    if (sixth != null) {
        overallLength += sixth.length;
    }

    Object[] joinedArray = (Object[]) Array.newInstance(first.getClass().getComponentType(), overallLength);
    System.arraycopy(first, 0, joinedArray, 0, first.length);
    System.arraycopy(second, 0, joinedArray, first.length, second.length);
    int copyTargetPosition = first.length + second.length;
    if (third != null) {
        System.arraycopy(third, 0, joinedArray, copyTargetPosition, third.length);
        copyTargetPosition += third.length;
    }
    if (fourth != null) {
        System.arraycopy(fourth, 0, joinedArray, copyTargetPosition, fourth.length);
        copyTargetPosition += fourth.length;
    }
    if (fifth != null) {
        System.arraycopy(fifth, 0, joinedArray, copyTargetPosition, fifth.length);
        copyTargetPosition += fifth.length;
    }
    if (sixth != null) {
        System.arraycopy(sixth, 0, joinedArray, copyTargetPosition, sixth.length);
    }
    return (T[]) joinedArray;
}

..and then there's an entry method for each combination of number of parameters (2..6), like so: ..然后有一个参数数量的每个组合的入口方法(2..6),如下所示:

public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second) {
    Preconditions.checkNotNull(first);
    Preconditions.checkNotNull(second);
    return mergeInternal(first, second, null, null, null, null);
}

public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third)
...
public static <T> T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third, @Nonnull T[] fourth)

And so on. 等等。

I think one rarely needs to merge more than 6 arrays and if you need to, you can always easily extend the given idea. 我认为很少需要合并超过6个数组,如果需要,您可以随时轻松扩展给定的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM