如何连接静态字符串数组

可能重复:
如何在Java中连接两个数组?

我将SET1声明为静态String [],我想将SET2声明为SET1 +其他几个参数。 是否可以将SET2静态相似(即私有静态String [])声明为SET1,但使用上面的定义,如果不是如何做到这一点?

private static final String[] SET1 = { "1", "2", "3" }; SET2 = SET1 + { "4", "5", "6" }; 

看看Commons Util ArrayUtils.add:

 static String[] SET2 = ArrayUtils.add(SET1, {"4", "5", "6" }); 

在这种情况下,列表可能更容易,因为数组的长度是固定的(本质上)。 如果要静态实例化,可以执行此类操作。

 private static final List SET1 = new ArrayList(); private static final List SET2 = new ArrayList(); static { SET1.add("1"); SET1.add("2"); SET2.addAll(SET1); SET2.add("3"); } 

或者使用某种Collection实用程序库。

这是一个很大的丑陋:

 private static final String[] SET1 = { "1", "2", "3" }; private static final String[] SET2 = concat( String.class, SET1, new String[]{"4", "5", "6"}); @SuppressWarnings("unchecked") static  T[] concat(Class clazz, T[] A, T[] B) { T[] C= (T[]) Array.newInstance(clazz, A.length+B.length); System.arraycopy(A, 0, C, 0, A.length); System.arraycopy(B, 0, C, A.length, B.length); return C; } 
 private static final String[] SET1 = { "1", "2", "3" }; private static final String[] SET2; static { List set2 = new ArrayList(Arrays.asList(SET1)); set2.addAll(Arrays.asList("3", "4", "5")); SET2 = set2.toArray(new String[0]); }