关于Java:如何连接静态字符串数组

How do I concatenate static string arrays

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
How to concatenate two arrays in Java?

我已经将set1声明为静态字符串[],我希望将set2声明为set1+少数其他参数。是否可以声明set2与set1静态相似(即private static string[])但使用上述定义,如果不是,如何这样做?

1
2
3
private static final String[] SET1 = {"1","2","3" };

SET2 = SET1 + {"4","5","6" };

Look at commons utile arrayutils.Add:

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


Maybe lists are easier in this case since arrays are fixed in length(by nature).如果你想静止地安装它,你可以这样做。

ZZU1

或使用某种收藏公用图书馆。


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

static
{
    List<String> set2 = new ArrayList<String>(Arrays.asList(SET1));
    set2.addAll(Arrays.asList("3","4","5"));
    SET2 = set2.toArray(new String[0]);
}

这是一个大傻瓜。

1
2
3
4
5
6
7
8
9
10
11
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> T[] concat(Class<T> 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;
 }