如何在String数组中转换Serializable?

How to convert Serializable in an array of String?

我想将一些字符串数组合并为一个。我使用了 ArrayUtils.addAll(T[], T...) 我在一些答案 here 上找到的。正如那里所描述的那样,我应该将它转换为一个字符串数组。当我尝试这样做时,它显示了这个错误

Cannot store java.io.Serializable in an array of java.lang.String at org.apache.commons.lang3.ArrayUtils.addAll

我的代码在这里

String[] splitLeft=split(left);
String[] middle=new String[]{with};
String[] splitRight=split(right);

String[] inWords=(String[])ArrayUtils.addAll(splitLeft,middle,splitRight);

这是什么问题,我该如何解决?

Ps: with 只是一个字符串。

这里的问题是the signature of the method是:

    addAll(T[] array1, T... array2)

因此第二个和第三个参数被视为 array2 的单个元素:它们没有连接;因此,推断类型为 Serializable,它是 String(第一个参数的元素类型)和 String[](可变参数的元素类型)的最小上限。

相反,如果您要使用 ArrayUtils.addAll:

,则必须加入他们的多个通话
addAll(addAll(splitLeft, middle), splitRight)

或者,您可以在少量语句中构建串联数组:

// Copy splitLeft, allocating extra space.
String[] inWords = Arrays.copyOf(splitLeft, splitLeft.length + 1 + splitRight.length);

// Add the "with" variable, no need to put it in an array first.
inWords[splitLeft.length] = with;

// Copy splitRight into the existing inWords array.
System.arraycopy(splitRight, 0, inWords, splitLength.length + 1, splitRight.length);