Apache Commons Lang library ArrayUtils.addAll(T[], T...)
就是專門幹這事的代碼:html
String[] both = ArrayUtils.addAll(first, second);
把下面的Foo
替換成你本身的類名java
public Foo[] concat(Foo[] a, Foo[] b) { int aLen = a.length; int bLen = b.length; Foo[] c= new Foo[aLen+bLen]; System.arraycopy(a, 0, c, 0, aLen); System.arraycopy(b, 0, c, aLen, bLen); return c; }
public <T> T[] concatenate (T[] a, T[] b) { int aLen = a.length; int bLen = b.length; @SuppressWarnings("unchecked") T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen+bLen); System.arraycopy(a, 0, c, 0, aLen); System.arraycopy(b, 0, c, aLen, bLen); return c; }
注意,泛型的方案不適用於基本數據類型(int,boolean……)git