Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up writing something of my own.</p> <p>There's a main method which does all the work:</p> <pre><code>@SuppressWarnings("unchecked") private static &lt;T&gt; 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; } </code></pre> <p>..and then there's an entry method for each combination of number of parameters (2..6), like so:</p> <pre><code>public static &lt;T&gt; T[] merge(@Nonnull T[] first, @Nonnull T[] second) { Preconditions.checkNotNull(first); Preconditions.checkNotNull(second); return mergeInternal(first, second, null, null, null, null); } public static &lt;T&gt; T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third) ... public static &lt;T&gt; T[] merge(@Nonnull T[] first, @Nonnull T[] second, @Nonnull T[] third, @Nonnull T[] fourth) </code></pre> <p>And so on.</p> <p>I think one rarely needs to merge more than 6 arrays and if you need to, you can always easily extend the given idea.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload